Find Control external button click event

5 posts, 1 answers
  1. Allan
    Allan avatar
    121 posts
    Member since:
    Jun 2008

    Posted 17 Mar 2011 Link to this post

    I would like to get the value of a textbox in a GridTemplateColumn. I have tried and tried to find an answer to this question. Please DO NOT refer me to any of the following:

    http://www.telerik.com/help/aspnet-ajax/grdaccessingcellsandrows.html

    http://demos.telerik.com/aspnet-ajax/grid/examples/programming/accessingcellsandrows/defaultcs.aspx

    http://www.telerik.com/community/forums/aspnet-ajax/grid/retrieving-value-from-radgrid-in-editmode-on-a-external-button-click.aspx

    http://www.telerik.com/community/forums/aspnet-ajax/grid/button-click-event-and-radgrid.aspx

    I have been to dozens and dozens of these links and NONE of them provide a simple, straight forward example to solve this. I have left several posts requesting similar examples with no resolution. Here is the code I am attempting to use:

    ASPX:

    <telerik:RadGrid ID="rgd_ABIUser" runat="server"  GridLines="None" AutoGenerateColumns="False" Width="400px" style="height: 46px">
        <MasterTableView DataKeyNames="ID">
            <RowIndicatorColumn>
            <HeaderStyle Width="20px"></HeaderStyle>
            </RowIndicatorColumn>
            <ExpandCollapseColumn>
            <HeaderStyle Width="20px"></HeaderStyle>
            </ExpandCollapseColumn>
                <Columns>
                    <telerik:GridTemplateColumn DataField="SC" HeaderText="Site" SortExpression="SC" UniqueName="SC">
                        <ItemTemplate>
                            <asp:TextBox ID="tbx_Site"  Text='<%# Bind("SC") %>' runat="server" Width="30px"></asp:TextBox>
                        </ItemTemplate>
                    </telerik:GridTemplateColumn>
                    <telerik:GridTemplateColumn DataField="ID" HeaderText="User" SortExpression="ID" UniqueName="ID">
                        <ItemTemplate>
                            <asp:TextBox ID="tbx_UserID"  Text='<%# Bind("ID") %>' runat="server"></asp:TextBox>
                        </ItemTemplate>
                    </telerik:GridTemplateColumn>
                    <telerik:GridTemplateColumn DataField="PW" HeaderText="Password" SortExpression="PW" UniqueName="PW">
                        <ItemTemplate>
                            <asp:TextBox ID="tbx_Password"  Text='<%# Bind("PW") %>' runat="server"></asp:TextBox>
                        </ItemTemplate>
                    </telerik:GridTemplateColumn>
                    <telerik:GridTemplateColumn DataField="SN" HeaderText="TCH#" SortExpression="SN" UniqueName="SN" >
                        <ItemTemplate>
                            <asp:TextBox ID="tbx_TchNo"  Text='<%# Bind("SN") %>' runat="server" Width="30px"></asp:TextBox>
                        </ItemTemplate>
                    </telerik:GridTemplateColumn>
                    <telerik:GridTemplateColumn DataField="TG" HeaderText="Group" SortExpression="TG" UniqueName="TG">
                        <ItemTemplate>
                            <asp:TextBox ID="tbx_TG"  Text='<%# Bind("TG") %>' runat="server" Width="20px"></asp:TextBox>
                        </ItemTemplate>
                    </telerik:GridTemplateColumn>
                </Columns>
        </MasterTableView>
    </telerik:RadGrid>

    Here is my ASPX.VB

    Protected Sub btn_UpdateUser_Click(sender As Object, e As System.EventArgs) Handles btn_UpdateUser.Click
        For Each item As GridDataItem In rgd_ABIUser.Items
            'Here is one way I have seen to get the value
            Dim txtBox As TextBox = DirectCast(item("PW").FindControl("tbx_Password"), TextBox)
            Dim PW As String = txtBox.Text
            'Here is a different way I have seen to get the value
            Dim SN As String = DirectCast(TryCast(item.FindControl("tbx_TchNo"), TextBox).Text, String)
            Dim TG As String = DirectCast(TryCast(item.FindControl("tbx_TG"), TextBox).Text, String)
            Dim DatabaseName As String = Session("DataBase")
            Dim Val = Session("Site")
            Dim Site As Integer = Convert.ToInt32(Val)
            Dim IDUser As String = Session("ID")
            Dim connectionString = New SqlConnection("server=DO-IT-AB\MSSQLAB;database=XXXX;UID=XXXX;PWD=XXX;")
            Dim command = New SqlCommand("aa_abi_users_update", connectionString)
            command.CommandType = CommandType.StoredProcedure
            command.Parameters.Add("@DBName", SqlDbType.VarChar).Value = DatabaseName
            command.Parameters.Add("@IDSite", SqlDbType.Int).Value = Site
            command.Parameters.Add("@IDUser", SqlDbType.VarChar).Value = IDUser
            command.Parameters.Add("@PW", SqlDbType.VarChar).Value = PW
            command.Parameters.Add("@SN", SqlDbType.VarChar).Value = SN
            command.Parameters.Add("@TG", SqlDbType.VarChar).Value = TG
            command.Connection.Open()
            command.ExecuteNonQuery()
            command.Connection.Close()
        Next

    Any help much appreciated.

  2. Answer
    Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 18 Mar 2011 Link to this post

    Hello Allan,

    Give a try with the following code snippet.
    C#:
    Protected Sub btn_UpdateUser_Click(sender As Object, e As EventArgs)
        For Each item As GridDataItem In rgd_ABIUser.MasterTableView.Items
            Dim txtBox As TextBox = DirectCast(item("SC").FindControl("tbx_Site"), TextBox)
            Dim s As String = txtBox.Text
        Next
    End Sub

    Thanks,
    Shinu.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Allan
    Allan avatar
    121 posts
    Member since:
    Jun 2008

    Posted 18 Mar 2011 Link to this post

    Unfortunately still no joy. Here is my modified code. I hard coded two of my variables just to focus on one at a time.

    Protected Sub btn_UpdateUser_Click(sender As Object, e As EventArgs)
        For Each item As GridDataItem In rgd_ABIUser.MasterTableView.Items
            Dim tbx_Password As TextBox = DirectCast(item("PW").FindControl("tbx_Password"), TextBox)
            Dim PW As String = tbx_Password.Text
            Dim DatabaseName As String = Session("DataBase")
            Dim Val = Session("Site")
            Dim Site As Integer = Convert.ToInt32(Val)
            Dim IDUser As String = Session("ID")
            Dim connectionString = New SqlConnection("server=DO-IT-AB\MSSQLAB;database=AeriesAdmin;UID=XXX;PWD=XXX;")
            Dim command = New SqlCommand("aa_abi_users_update", connectionString)
            command.CommandType = CommandType.StoredProcedure
            command.Parameters.Add("@DBName", SqlDbType.VarChar).Value = DatabaseName
            command.Parameters.Add("@IDSite", SqlDbType.Int).Value = Site
            command.Parameters.Add("@IDUser", SqlDbType.VarChar).Value = IDUser
            command.Parameters.Add("@PW", SqlDbType.VarChar).Value = PW
            command.Parameters.Add("@SN", SqlDbType.VarChar).Value = "123"
            command.Parameters.Add("@TG", SqlDbType.VarChar).Value = "T"
            command.Connection.Open()
            command.ExecuteNonQuery()
            command.Connection.Close()
        Next
  5. Allan
    Allan avatar
    121 posts
    Member since:
    Jun 2008

    Posted 18 Mar 2011 Link to this post

    Okay. I can admit it when it is my fault.

    I should have have qualified If Not Postback:

    Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        If Not Page.IsPostBack Then
            rgd_ABIUser.DataSource = GetUser()
            rgd_ABIUser.DataBind()
        End If
    End Sub

    Sometimes one can't see the forest for the trees.
  6. Allan
    Allan avatar
    121 posts
    Member since:
    Jun 2008

    Posted 18 Mar 2011 Link to this post

    Shinu,

    I am going to mark your answer as the answer because your answer was correct even though it was not the solution. I do not like having posts show as unanswered when in fact they are answered. I would like to highly suggest that Telerik allow a posting user the ability to mark a response they make as the answer.

    I have solved several of my own posts and ruturned to update my post with the solution. However, because I cannot mark my own response as the answer, the post looks as though it has never been resolved even though it has been.

    I am sure many novice users of this forum skip posts similar to their own issue because the post shows as unanswered even though, as in my case, I have provided the solution.
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017