Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

How to put a value into an <EditItemTemplate><asp:TextBox> with JavaScript

Posted on 2012-08-23
6
Medium Priority
?
871 Views
Last Modified: 2012-08-23
Partial Class wpAdmin
    Inherits System.Web.UI.Page

    Dim iUserName As String = "UserName"

    Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load

        'iUserName = Right(Request.ServerVariables("AUTH_USER"), (Request.ServerVariables("AUTH_USER").Length - 5))  ' Remove the five characters: "BETA\"
        iUserName = Request.LogonUserIdentity.Name.Substring(Request.LogonUserIdentity.Name.LastIndexOf("\") + 1)    ' Both of these produce the same UserName minus domain.
        lblUsername.Text = iUserName

    End Sub
End Class

Open in new window

How to put logged in user name into txtProcessedBy? (TextBox on Line 9)
  
<asp:Label ID="lblUsername" runat="server" Text="UserName" />

<asp:GridView ID="gv" runat="server" DataSourceID="SqlDataSource1" DataKeyNames="CustomerID">
<Columns>
                <asp:CommandField ShowEditButton="True" UpdateText="Save" />              
                <asp:TemplateField HeaderText="ProcessedBy" SortExpression="ProcessedBy">
                    <EditItemTemplate>
                        <asp:TextBox ID="txtProcessedBy" runat="server" Text='<%# Bind("ProcessedBy") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="lblProcessedBy" runat="server" Text='<%# Eval("ProcessedBy") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>

Open in new window

Thanks in advance.
0
Comment
Question by:megnin
  • 4
6 Comments
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 38325633
Is javascript a requirement? You can handle the RowEditing event of the gridview and put this value in textbox.

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowediting.aspx
0
 
LVL 1

Author Comment

by:megnin
ID: 38325647
No, I just thought it may be the simplest.  Of course I could be wrong.  ;-)
0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 2000 total points
ID: 38325670
The short answer is:  you can't. Templated items run on the server and Javascript runs on the client.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:megnin
ID: 38325690
I've already looked at that page.  I'm not sure if I want to scrap my project and start over building it around that example.  I have a simple GridView control on an ASP.Net page.  I didn't build it in codebehind.
I'm been trying to figure out how to put one value in a TextBox for two full days.  My brain is fried.  I'm sure that example is very simple to you, but I can't even figure out what applies to my problem and what doesn't.  I'm sorry.
0
 
LVL 1

Author Comment

by:megnin
ID: 38325708
Hi kaufmed.  Ah, that explains why I can't find any examples by Googling.  ;-)

Is there is simple way to...

If gv.IsInEditMode
  txtProcessedBy.Text = lblUserName.Text
End If
0
 
LVL 1

Author Comment

by:megnin
ID: 38325721
Everything commented out (which is everything) I've tried and produces some error.

Imports System.Data
Imports System.Data.SqlClient
Imports System.Web
Imports System.Web.UI.WebControls

Partial Class wpAdmin
    Inherits System.Web.UI.Page

    Dim iUserName As String = "UserName"
    Dim varFirstName As String
    Dim varLastName As String

    Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load

        'iUserName = Right(Request.ServerVariables("AUTH_USER"), (Request.ServerVariables("AUTH_USER").Length - 5))  ' Remove the five characters: "BETA\"
        iUserName = Request.LogonUserIdentity.Name.Substring(Request.LogonUserIdentity.Name.LastIndexOf("\") + 1)    ' Both of these produce the same UserName minus domain.
        lblUsername.Text = iUserName

    End Sub
    'Protected Sub SqlDataSource1_Updating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.SqlDataSourceCommandEventArgs) Handles SqlDataSource1.Updating

    '    e.Command.Parameters("ProcessedBy").Value = My.User.Name

    'End Sub

    '  Protected Sub gv_RowCommand(sender As Object, e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles gv.RowCommand

    ' 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 label outside of gridview
    'row.Cells(13).Text = Me.lblUsername.Text

    'Dim tb As TextBox = CType(gv.FindControl("txtProcessedBy"), TextBox)
    'tb.Text = Me.lblUsername.Text

    'Dim tb As TextBox = CType(row.FindControl("txtProcessedBy"), TextBox)
    'tb.Text = Me.lblUsername.Text

    'If row.RowState = DataControlRowState.Edit Then
    '    'Dim tb As TextBox = DirectCast(sender.Rows(index).FindControl("txtProcessedBy"), TextBox)
    '    Dim tb As TextBox = CType(gv.FindControl("txtProcessedBy"), TextBox)
    '    tb.Text = Me.lblUsername.Text
    'Else
    '    row.Cells(13).Text = Me.lblUsername.Text
    'End If



    ' if label in the gridview
    ' row.Cells(1).Text = row.Cells(x).Text ' x=column index, first column index=0
    ' End Sub

    'Protected Sub Processed_CheckedChanged(sender As Object, e As System.EventArgs)
    '    ' change "Anything" to "Update" if you want the gridview to Update the data source, or you can invoke the update later by calling gv.databind()

    '    Dim CommandEventArgs As New CommandEventArgs("Update", sender.namingcontainer.DataItemIndex)
    '    Dim GridViewCommandEventArgs As New GridViewCommandEventArgs(sender, CommandEventArgs)
    '    gv_RowCommand(gv, GridViewCommandEventArgs)
    'End Sub

    ' '' ### Just didn't work --->>Protected Sub gv_RowDataBound(sender As Object, e As GridViewRowEventArgs)
    ' '' ##################  This is the closest so far.  UserName goes into the TextBox but the db is not updated.
    '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 on 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




    'Protected Sub gv_RowCommand(sender As Object, e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles gv.RowCommand

    '    If (row.RowType = DataControlRowType.DataRow) AndAlso ((row.RowState And DataControlRowState.Edit) > 0) Then
    '        Dim objCheckBox As CheckBox = Nothing
    '        Dim objTextBox As TextBox = Nothing

    '        objCheckBox = TryCast(row.FindControl("cbProcessed"), CheckBox)

    '        If objCheckBox IsNot Nothing Then
    '            With objCheckBox
    '                If .Checked = True Then
    '                    objTextBox = TryCast(row.FindControl("txtProcessedBy"), TextBox)
    '                    If objTextBox IsNot Nothing Then
    '                        With objTextBox
    '                            .Text = Me.lblUsername.Text
    '                            .Dispose()
    '                        End With
    '                    End If
    '                End If
    '                .Dispose()
    '            End With
    '        End If
    '    End If

    '    gv.UpdateRow(sender.Rows(index), True)

    'End Sub




    'Protected Sub cbProcessed_CheckedChanged(sender As Object, e As System.EventArgs)
    '    Dim cbProcessed As CheckBox = DirectCast(sender, CheckBox)
    '    Dim row As GridViewRow = DirectCast(cbProcessed.NamingContainer, GridViewRow)
    '    Dim cid As String = row.Cells(1).Text
    '    Dim status As Boolean = cbProcessed.Checked

    '    Dim query As String = "UPDATE ReturnToWork SET Processed = @Processed, ProcessedBy = @ProcessedBy WHERE CustomerID = @CustomerID"
    '    Dim con As New SqlConnection(ConfigurationManager.ConnectionStrings("WPConnectionString").ConnectionString)
    '    Dim com As New SqlCommand(query, con)

    '    Try
    '        com.Parameters.Add("@Processed", SqlDbType.Bit).Value = status
    '        com.Parameters.Add("@ProcessedBy", SqlDbType.VarChar).Value = iUserName
    '        com.Parameters.Add("@CustomerID", SqlDbType.VarChar).Value = cid

    '        con.Open()
    '        com.ExecuteNonQuery()
    '        con.Close()

    '    Catch ex As Exception
    '        Response.Write(ex)
    '        'Server.Transfer("Error.htm")
    '    End Try
    '    'Response.Redirect(Request.RawUrl)

    'End Sub

    'Protected Sub gv_RowDataBound(sender As Object, e As GridViewRowEventArgs)
    '    If e.Row.RowType = DataControlRowType.DataRow Then
    '        Dim chkbox As CheckBox = TryCast(e.Row.FindControl("cbProcessed"), CheckBox)
    '        'If chkbox Is Not Nothing Then
    '        chkbox.Attributes.Add("OnClick", "javascript:SetValue(this.id);")
    '        'End If
    '    End If
    'End Sub


End Class

Open in new window

0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

578 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question