This is a migrated thread and some comments may be shown as answers.

Find Control external button click event

4 Answers 181 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Allan
Top achievements
Rank 2
Allan asked on 17 Mar 2011, 07:04 PM
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.

4 Answers, 1 is accepted

Sort by
0
Accepted
Shinu
Top achievements
Rank 2
answered on 18 Mar 2011, 05:54 AM
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.
0
Allan
Top achievements
Rank 2
answered on 18 Mar 2011, 03:06 PM
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
0
Allan
Top achievements
Rank 2
answered on 18 Mar 2011, 06:18 PM
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.
0
Allan
Top achievements
Rank 2
answered on 18 Mar 2011, 06:26 PM
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.
Tags
Grid
Asked by
Allan
Top achievements
Rank 2
Answers by
Shinu
Top achievements
Rank 2
Allan
Top achievements
Rank 2
Share this question
or