gridview: filling a textbox with a value of a field by clicking on a button in that field

Rate It (1)

Last post 02-08-2008 4:38 AM by BenJ83. 6 replies.

Sort Posts:

  • gridview: filling a textbox with a value of a field by clicking on a button in that field

    02-06-2008, 10:52 AM
    • Member
      29 point Member
    • BenJ83
    • Member since 01-15-2008, 7:04 AM
    • Belgium
    • Posts 46

    Hi,

    I have created a gridview filled with data from a database. Now when I go into edit mode, I want to click on button1 in the Templatefield "text" and the text should be shown in a textbox below the grid ready to edit. But I get errors when I want to do an onclick event with the button, saying "Id Expected". 

    I also don't know how to get the value of text into a variable or which variable contains the value of "text" and how to pass it to the textbox below.

    Maybe there's a better way to do this then I have in mind?

    thx in advance.

     

    <asp:TemplateField HeaderText="text" SortExpression="text">

    <ItemTemplate>

    <asp:Label ID="Label1" runat="server" Width="250" Height="50" Text='<%# Eval("text") %>' />

    </ItemTemplate>

    <EditItemTemplate>

    <asp:Label ID="Label2" runat="server" Width="250" Height="50" Text='<%# Bind("text") %>' />

    </EditItemTemplate>

    <EditItemTemplate>

    <asp:Button ID="Button1" runat="server" Text="Edit" OnClick="<% display_text(text) %>" />

    </EditItemTemplate>

    </asp:TemplateField>

  • Re: gridview: filling a textbox with a value of a field by clicking on a button in that field

    02-06-2008, 12:41 PM
    Answer
    • Contributor
      4,757 point Contributor
    • naveenj
    • Member since 07-02-2007, 9:09 AM
    • India
    • Posts 863

    Hi BenJ83,

    OnEditMode you have a textbox and a button.
    OnClicking the Button you want to fill the textbox with your original value,right?

    This will do.

    <asp:TemplateField>
        <ItemTemplate>
            <asp:Label ID="lblText" runat="server"
                        Text='<%#Eval("text") %>'>
            </asp:Label>
        </ItemTemplate>
        <EditItemTemplate>
            <asp:TextBox ID="TextBox1" runat="server">
            </asp:TextBox>
            <asp:Button ID="Button1" runat="server"
                        Text="FillText"
                        CommandArgument='<%#Eval("text") %>'
                        OnCommand="Button1_Command" />
        </EditItemTemplate>
    </asp:TemplateField>

    protected void Button1_Command(object sender, CommandEventArgs e)
    {
        GridViewRow gvr =((Button)sender).Parent.Parent as GridViewRow;
        ((TextBox)gvr.FindControl("TextBox1")).Text = e.CommandArgument.ToString();
    }

    Regards,
    Naveen

    Please remember to click Mark as Answer on the post that helps you
    View Blog
  • Re: gridview: filling a textbox with a value of a field by clicking on a button in that field

    02-07-2008, 2:59 AM
    • Member
      29 point Member
    • BenJ83
    • Member since 01-15-2008, 7:04 AM
    • Belgium
    • Posts 46

    Hi Naveenj,

     thx for your anser. I wanted to fill a textbox1.text outside the gridview and now I want to edit the text in there because the text in the gridview is too long to edit in the gridview.

    Your solution is the right way. I only needed to assign the e.CommandArgument.ToString() to the Textbox.text. to fill it.

    But I got another problem now. I'm using VB and want to assign the edited value of the textbox outside the gridview back into the gridview into label2 (by clicking another button below textbox1) where I clicked the button. I know your code does the trick in C# for the button in the gridview but I'm using VB and I don't know how the value of the row which is selected in the gridview.

     thx for your help, already helped me a lot.

     

  • Re: gridview: filling a textbox with a value of a field by clicking on a button in that field

    02-07-2008, 3:21 AM
    • Member
      29 point Member
    • BenJ83
    • Member since 01-15-2008, 7:04 AM
    • Belgium
    • Posts 46

    I've got something like this now, but it gives me an error.

     

    Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click

             Dim gvr As GridViewRow = GridView1.FindControl("Label2").Parent.Parent

             ((Textbox)gvr.FindControl("Label2")).Text = TextBox1.Text

    End Sub

    I click on the button and want to put TextBox1.Text back into label2.Text of the gridview.

    But VB doesn't know this syntax:

    "((Textbox)gvr.FindControl("Label2")).Text"

  • Re: gridview: filling a textbox with a value of a field by clicking on a button in that field

    02-07-2008, 9:00 AM
    • Contributor
      4,757 point Contributor
    • naveenj
    • Member since 07-02-2007, 9:09 AM
    • India
    • Posts 863

     Hi BenJ83,

    Try  this to change code. http://www.codechanger.com/

    Its something like

    (DirectCast(gvr.FindControl("Label2"), Textbox)).Text = TextBox1.Text 

    Regards,
    Naveen

    Please remember to click Mark as Answer on the post that helps you
    View Blog
  • Re: gridview: filling a textbox with a value of a field by clicking on a button in that field

    02-07-2008, 10:08 AM
    • Member
      29 point Member
    • BenJ83
    • Member since 01-15-2008, 7:04 AM
    • Belgium
    • Posts 46

    thx for replying again Naveenj,

     This should be the code afaik:

    Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click

         Dim gvr As GridViewRow = TryCast((DirectCast(sender, Button)).Parent.Parent, GridViewRow)

         DirectCast(gvr.FindControl("Label2"), Label).Text = TextBox1.Text

    End Sub

    The problem now is that sender in the first line has to be the button1 in the gridview. Now it is button2 below the textbox1 that needs to send the edited text back to the gridview.

     I tried to declare a public variable:

    Public obj as Object

    and assign:

    obj = sender 

    at the eventclick of button1.

    But the value of obj is lost when the button2 event fires. So if I can keep the value of  sender from button1 my problem seems to be solved.

  • Re: gridview: filling a textbox with a value of a field by clicking on a button in that field

    02-08-2008, 4:38 AM
    • Member
      29 point Member
    • BenJ83
    • Member since 01-15-2008, 7:04 AM
    • Belgium
    • Posts 46

    I solved my problem. I've put 2 buttons in my templatefield. 1 to bring the text outside the gridview into a textbox and one to bring back the edited text in the textbox.

     My templatefield:

    <asp:TemplateField HeaderText="text" SortExpression="text">

    <ItemTemplate>

    <asp:Label ID="Label1" runat="server" Width="250" Height="70" Text='<%# Eval("text") %>' />

    </ItemTemplate>

    <EditItemTemplate>

    <asp:Label ID="Label2" runat="server" Width="250" Height="50" Text='<%# Bind("text") %>' /><br />

    <table><tr>

    <td><asp:Button ID="Button1" runat="server" Text="Edit" Width="125" Height="20" CommandArgument='<%# Bind("text") %>' OnCommand="Button1_Command" /></td>

    <td><asp:Button ID="Button2" runat="server" Text="Save" Width="125" Height="20" Enabled="false" CommandArgument='<%# Bind("Id") %>' OnCommand="Button2_Command" /></td>

    </tr></table>

    </EditItemTemplate>

    </asp:TemplateField>

     

    My code behind of the 2 OnCommands:

    Public Sub Button1_Command(ByVal sender As Object, ByVal e As CommandEventArgs)

    TextBox1.Visible = True

    TextBox1.Text = e.CommandArgument.ToString()

    Dim gvr As GridViewRow = TryCast((DirectCast(sender, Button)).Parent.Parent, GridViewRow)

    DirectCast(gvr.FindControl("Button2"), Button).Enabled = True

    End Sub

    Public Sub Button2_Command(ByVal sender As Object, ByVal e As CommandEventArgs)

    Dim gvr As GridViewRow = TryCast((DirectCast(sender, Button)).Parent.Parent, GridViewRow)

    DirectCast(gvr.FindControl("Label2"), Label).Text = TextBox1.Text

    DirectCast(gvr.FindControl("Button2"), Button).Visible = False

    TextBox1.Visible = False

    Dim congroepboden As SqlConnection

    Dim strconString As String

    Dim cmdselect As SqlCommand

    strconString = System.Configuration.ConfigurationManager.AppSettings("conString")

    congroepboden = New SqlConnection(strconString)

    strconString = "Update boden_klantverhalen set text = '" & TextBox1.Text & "' where Id = " & e.CommandArgument.ToString()

    cmdselect = New SqlCommand(strconString, congroepboden)

    congroepboden.Open()

    cmdselect.ExecuteNonQuery()

    congroepboden.Close()

    GridView1.DataBind()

    End Sub

Page 1 of 1 (7 items)
Microsoft Communities