David Megnin
asked on
Set the value of a TextBox in a Gridview
I have a simple ASP.Net 4.0 Gridview ID="gv".
The column, "Processed By", DataField="ProcessedBy"
The label, "lblUserName" has the value of the logged in user.
When "Edit" is clicked on a row I need the value of lblUserName to go into "Processed By".
If it's easier to do as a TemplateField, then the TextBox ID will be "txtProcessedBy".
The column, "Processed By", DataField="ProcessedBy"
The label, "lblUserName" has the value of the logged in user.
When "Edit" is clicked on a row I need the value of lblUserName to go into "Processed By".
If it's easier to do as a TemplateField, then the TextBox ID will be "txtProcessedBy".
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I found this which puts the UserName in the TextBox okay, but then it's not saved to the database. I click Save/Update but the TextBox goes back to being blank.
With that Sub commented out, a value typed into the TextBox is saved to the db.
''Protected Sub gv_RowDataBound(sender As Object, e As GridViewRowEventArgs)
Protected Sub gv_RowDataBound(sender As Object, e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gv.RowDataBound
Dim row As GridViewRow = e.Row
If (row.RowType = DataControlRowType.DataRow) AndAlso ((row.RowState And DataControlRowState.Edit) > 0) Then
Dim txt As TextBox = TryCast(row.FindControl("txtProcessedBy"), TextBox)
' Do your thing here
'If txt <> Nothing Then
'End If
txt.Text = Me.lblUsername.Text
End If
End Sub
With that Sub commented out, a value typed into the TextBox is saved to the db.
Possible that updateRow is not being triggered, for whatever reason.
txt.Text = Me.lblUsername.Text
gv.UpdateRow(sender.Rows(index), True)
Alan ";0)
ASKER
Can't access "index" because it is "Friend". Squiggly under "index".
Hi megnin,
I expect you'll be glad to get this one off your desk!
I expect you'll be glad to get this one off your desk!
Protected Sub gv_RowDataBound(sender As Object, e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gv.RowDataBound
' get the commandargument
Dim index As Integer = Convert.ToInt32(e.CommandArgument)
' Retrieve the row that contains the checkbox checked
' by the user from the Rows collection.
Dim row As GridViewRow = sender.Rows(index)
If (row.RowType = DataControlRowType.DataRow) AndAlso ((row.RowState And DataControlRowState.Edit) > 0) Then
Dim txt As TextBox = TryCast(row.FindControl("txtProcessedBy"), TextBox)
' Do your thing here
'If txt <> Nothing Then
'End If
txt.Text = Me.lblUsername.Text
End If
End Sub
Alan ";0)
ASKER
Ha, yeah. ;)
Error 1 'CommandArgument' is not a member of 'System.Web.UI.WebControls .GridViewR owEventArg s'.
Error 1 'CommandArgument' is not a member of 'System.Web.UI.WebControls
ASKER
If I do it like this, the value goes into the TextBox when user clicks "Edit" but the value is not updated in the database. WHen user clicks "Save" the TextBox is blank again.
Protected Sub gv_RowDataBound(sender As Object, e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gv.RowDataBound
' ' get the commandargument
' Dim index As Integer = Convert.ToInt32(e.CommandArgument) ' <---Error
' ' Retrieve the row that contains the checkbox checked
' ' by the user from the Rows collection.
Dim row As GridViewRow = e.Row ' sender.Rows(index) ' <--- index causes error
If (row.RowType = DataControlRowType.DataRow) AndAlso ((row.RowState And DataControlRowState.Edit) > 0) Then
Dim txt As TextBox = TryCast(row.FindControl("txtProcessedBy"), TextBox)
' Do your thing here
'If txt <> Nothing Then ' <-- <> causes error
'End If
txt.Text = Me.lblUsername.Text
End If
End Sub
Did you try http:#a38319415 ?
Hi megnin,
Add a new sub to handle the textbox databinding event:
In your gridview attach the event:
When "Edit" is clicked on a row I need the value of lblUserName to go into "Processed By"
Add a new sub to handle the textbox databinding event:
Protected Sub txtProcessedBy_DataBinding(sender As Object, e As System.EventArgs)
sender.text = My.User.Name
End Sub
In your gridview attach the event:
<EditItemTemplate>
<asp:TextBox ID="txtProcessedBy" runat="server" Text='<%# Bind("ProcessedBy") %>' OnDataBinding="txtProcessedBy_DataBinding"></asp:TextBox>
</EditItemTemplate>
Alan ";0)
ASKER
Thank you very much. The solution I finally used was very similar. I learned a lot working on this one. In Microsoft SQL Server Management Studio, the "Use Optomistic Concurrency" option is not always your friend.
ASKER
Open in new window
Here is everything I've tried that hasn't worked. Most is commented out so it's hard to tell what's what as here are so many failed tries:
Open in new window