I have changed my code and first I go thru and find the checked ones then I want to bind with dropdown
So, I have done this and it does give me two key values, if I checked two checkboxes, but on my second for each loop, it is giving me error, because probably I didn't set that correct format
Dim key
As DataKey
Dim row As GridViewRow
For Each row
In GridView1.Rows
Dim result
As Boolean = (CType(row.FindControl("Chkerr"),
CheckBox)).Checked
If (result)
Then
key = GridView1.DataKeys(row.RowIndex)
Response.Write(CType(key.Value,
Integer))
>>>>> (gives me error) For
Each key In GridView1.Rows
If GridView1.DataKeys(row.RowIndex).Value.ToString() <> 21
Then
since my first part of code gives me two or more keys, I need to see if values of each key = 21 or not if it is not <>21 I want this row to be edit mode. How can I do that for the second part of code?
I have tried this way, and even though I set my txtmessage = "" still at the end, I see two values in that text box,why is that?
Dim key As DataKey
'Dim row As GridViewRow
'For Each row In GridView1.Rows
Dim result As Boolean = (CType(row.FindControl("Chkerr"), CheckBox)).Checked
'If (result) Then
'If txtmessage.Text <> "" Then
'txtmessage.Text = ""
'End If
key = GridView1.DataKeys(row.RowIndex)
Response.Write(CType(key.Value, Integer))
txtmessage.Text = (CType(key.Value, Integer))
'For Each key In GridView1.DataKeys >>> I comment it out that line
If GridView1.DataKeys(row.RowIndex).Value.ToString() <> 21 Then
GridView1.EditIndex = row.RowIndex
Adding Controls at the Right Time
We already know that when adding controls dynamically through the page's code portion the controls must be added on every postback. But when in the page lifecycle should the controls be added? At first guess, we might decide to put such code in the
Page_Load
event handler, causing the controls to be added during the Load stage of the page's lifecycle. This would work fine if we don't need to worry about saving the controls' view state across postbacks, but if we do need to persist the view state of the dynamically added controls the Load stage is not where we should be adding these controls.
If we need our dynamically added controls to maintain their view state it is paramount that these controls be added before the Load View State stage. That is, these controls must exist within the page's control hierarchy before the view state is loaded. There's only one stage before Load View State - Initialization. That means, if we want our dynamic controls to persist view state we must add them to the control hierarchy in the page's
Init
event.
If you are using Visual Studio .NET, the code-behind class already contains an event handler for the page's
Init
event. This event handler (named
Page_Init
in VB.NET code-behind classes and
OnInit
in C# code-behind classes) is tucked away in the hidden "Web Form Designer Generated Code" region. What I typically do is create a separate method in the code-behind class and simply call this method from the
Init
event
handler. Once you have configured everything so that the dynamic controls are added during the Initialization stage, you can then read/write the controls' properties in the Load stage. You'll find that the users' interactions with the dynamic controls remains
"remembered" across postbacks if you follow this pattern.
jaws1021
Member
50 Points
189 Posts
Re: checkbox and viewstate
Jun 20, 2007 12:53 AM|LINK
I have changed my code and first I go thru and find the checked ones then I want to bind with dropdown
So, I have done this and it does give me two key values, if I checked two checkboxes, but on my second for each loop, it is giving me error, because probably I didn't set that correct format
Dim key As DataKey Dim row As GridViewRow For Each row In GridView1.Rows Dim result As Boolean = (CType(row.FindControl("Chkerr"), CheckBox)).Checked If (result) Thenkey = GridView1.DataKeys(row.RowIndex)
Response.Write(CType(key.Value, Integer)) >>>>> (gives me error) For Each key In GridView1.Rows If GridView1.DataKeys(row.RowIndex).Value.ToString() <> 21 ThenGridView1.EditIndex = row.RowIndex
BindGrid(2424, "M", 1) End If Next End If Nextysw
Contributor
2784 Points
515 Posts
Re: checkbox and viewstate
Jun 20, 2007 07:47 AM|LINK
GridView.Rows collection is a collection of a GridViewRow. You try to use variable key which is DataKey.
Did you mean
for each row in GridView1.Rows?
jaws1021
Member
50 Points
189 Posts
Re: checkbox and viewstate
Jun 20, 2007 05:04 PM|LINK
since my first part of code gives me two or more keys, I need to see if values of each key = 21 or not if it is not <>21 I want this row to be edit mode. How can I do that for the second part of code?
jaws1021
Member
50 Points
189 Posts
Re: checkbox and viewstate
Jun 20, 2007 06:38 PM|LINK
I have tried this way, and even though I set my txtmessage = "" still at the end, I see two values in that text box,why is that?
Dim key As DataKey
'Dim row As GridViewRow 'For Each row In GridView1.Rows Dim result As Boolean = (CType(row.FindControl("Chkerr"), CheckBox)).Checked 'If (result) Then 'If txtmessage.Text <> "" Then 'txtmessage.Text = "" 'End If key = GridView1.DataKeys(row.RowIndex) Response.Write(CType(key.Value, Integer)) txtmessage.Text = (CType(key.Value, Integer)) 'For Each key In GridView1.DataKeys >>> I comment it out that line
If GridView1.DataKeys(row.RowIndex).Value.ToString() <> 21 Then GridView1.EditIndex = row.RowIndex
End If
End If Nextysw
Contributor
2784 Points
515 Posts
Re: checkbox and viewstate
Jun 20, 2007 09:47 PM|LINK
I am not sure what you are asking..
Response.Write(CType(key.Value, Integer)) - writeys some value to response. where do you expect to get it?
ysw
Contributor
2784 Points
515 Posts
Re: checkbox and viewstate
Jun 20, 2007 09:47 PM|LINK
I am not sure what you are asking..
Response.Write(CType(key.Value, Integer)) - writeys some value to response. where do you expect to get it?
Rex Lin - MS...
All-Star
17422 Points
2116 Posts
Re: checkbox and viewstate
Jun 21, 2007 01:19 AM|LINK
HI, jaws1021:
Adding Controls at the Right Time
event handler, causing the controls to be added during the Load stage of the page's lifecycle. This would work fine if we don't need to worry about saving the controls' view state across postbacks, but if we do need to persist the view state of the dynamically added controls the Load stage is not where we should be adding these controls.We already know that when adding controls dynamically through the page's code portion the controls must be added on every postback. But when in the page lifecycle should the controls be added? At first guess, we might decide to put such code in the
If we need our dynamically added controls to maintain their view state it is paramount that these controls be added before the Load View State stage. That is, these controls must exist within the page's control hierarchy before the view state is loaded. There's only one stage before Load View State - Initialization. That means, if we want our dynamic controls to persist view state we must add them to the control hierarchy in the page's
event.If you are using Visual Studio .NET, the code-behind class already contains an event handler for the page's
event. This event handler (named in VB.NET code-behind classes and in C# code-behind classes) is tucked away in the hidden "Web Form Designer Generated Code" region. What I typically do is create a separate method in the code-behind class and simply call this method from the event handler. Once you have configured everything so that the dynamic controls are added during the Initialization stage, you can then read/write the controls' properties in the Load stage. You'll find that the users' interactions with the dynamic controls remains "remembered" across postbacks if you follow this pattern.Here are some corresponding material for you:
http://aspnet.4guysfromrolla.com/articles/092904-1.aspx
http://msdn2.microsoft.com/en-us/library/hbdfdyh7.aspx
I hope the above information will be helpful. If you have any issues or concerns, please let me know. It's my pleasure to be of assistance
__________________________________________________
Sincerely,
Rex Lin
Microsoft Online Community Support
If there is any question or the issue is not resolved, please feel free to mark the thread as not resolved