Page view counter

Retrieving GridView nested EditItemTemplate TextBox control value

Last post 10-08-2008 4:28 AM by Qin Dian Tang - MSFT. 6 replies.

Sort Posts:

  • Retrieving GridView nested EditItemTemplate TextBox control value

    10-04-2008, 6:14 PM
    • Loading...
    • oldbcells
    • Joined on 09-18-2008, 1:33 AM
    • Posts 6
    • Points 1

     I have a lookup table parsed to a gridview. all good

    The gridview has update and insert capabilities. all good

    On update(edit) of an item in the lookup table I would like to also update any records of a main inventory table that contains the old lookup value.

    Specifically, how can I retrieve the initial column value from the gridview prior to edit to pass on to an updatecommand?

    <EditItemTemplate>                         
    <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Name") %>'></asp:TextBox>
    </EditItemTemplate>
    <ItemTemplate>
    <asp:Label ID="Label1" runat="server" Text='<%# Bind("Name") %>'></asp:Label>
    </ItemTemplate>

     

  • Re: Retrieving GridView nested EditItemTemplate TextBox control value

    10-05-2008, 2:05 AM
    • Loading...
    • Bohunkus
    • Joined on 05-22-2006, 10:13 PM
    • San Jose, Ca.
    • Posts 565
    • Points 2,552

      You need to hook into the RowUpdating event for the GridView. Click on the Gridview and then in the Properties window, select the events button (lightening bolt) to find the RowUpdating event listed. Double click to the right of the RowUpdating label to create the event in your code-behind. Once you have the code-behind, use the following code to access the fields in the row being edited. Once you have access to the fields, you can do anything you need to (like save the values to another place).

        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            string myFirstName = e.NewValues[0].ToString();
            string myLastName = e.NewValues[1].ToString();
            // save values to other places
            //  .....
        }

    HTHs!!

    Cheers,
    Chris

    Remember, you can mark more than 1 post as the answer.
  • Re: Retrieving GridView nested EditItemTemplate TextBox control value

    10-05-2008, 11:47 AM
    • Loading...
    • oldbcells
    • Joined on 09-18-2008, 1:33 AM
    • Posts 6
    • Points 1

    I am using OnRowCommand as my event handler..here is code:

    Page code:

    <asp:GridView ID="GridViewMUNICIPALITY"
                OnRowCommand="GridViewMUNICIPALITY_OnRowCommand"
                runat="server" AllowSorting="True"
                AutoGenerateColumns="False" DataKeyNames="IDnum"
                DataSourceID="SqlDataSourceMUNICIPALITY" ShowFooter="True" CellPadding="5" CellSpacing="5" Font-Bold="True" Font-Size="Large" HorizontalAlign="Right">
                    <Columns>
                        <asp:TemplateField ShowHeader="True" HeaderStyle-Font-Bold="True" HeaderStyle-Font-Size="Large" HeaderText="Municipality Items">
                            <EditItemTemplate>
                                <asp:LinkButton ID="LinkButtonMUN1" runat="server" CausesValidation="True" CommandName="Update" Text="Update"></asp:LinkButton>
                                &nbsp;<asp:LinkButton ID="LinkButtonMUN2" runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancel"></asp:LinkButton>
                            </EditItemTemplate>
                            <ItemTemplate>
                                <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Edit" Text="Edit"></asp:LinkButton>
                                &nbsp;<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="Delete" Text="Delete"></asp:LinkButton>
                            </ItemTemplate>
                            <FooterTemplate>
                                <asp:LinkButton ID="LinkButtonInsertMUN" CommandName="Insert" runat="server">Add</asp:LinkButton>
                                <asp:LinkButton ID="LinkButtonCancelMUN" CommandName="Cancel" runat="server">Cancel</asp:LinkButton>
                            </FooterTemplate>
                        </asp:TemplateField>
                        <asp:BoundField DataField="IDnum" HeaderText="IDnum" InsertVisible="False" ReadOnly="True" SortExpression="IDnum" />
                        <asp:TemplateField HeaderText="Name" SortExpression="Name">
                            <EditItemTemplate>
                                <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Name") %>'></asp:TextBox>
                            </EditItemTemplate>
                            <ItemTemplate>
                                <asp:Label ID="Label1" runat="server" Text='<%# Bind("Name") %>'></asp:Label>
                            </ItemTemplate>
                            <FooterTemplate>
                                <asp:TextBox ID="TextBoxMUNINSERT" Text='<%# Bind("Name") %>' runat="server"></asp:TextBox>
                            </FooterTemplate>
                        </asp:TemplateField>
                    </Columns>
                </asp:GridView>

     

    CODE BEHIND:

    // MUNICIPALITY LOOKUP TABLE INSERT
            protected void GridViewMUNICIPALITY_OnRowCommand(object sender, GridViewCommandEventArgs e)
            {

               if (e.CommandName == "Insert") // this works fine
                {
                    TextBox Name = GridViewMUNICIPALITY.FooterRow.FindControl("TextBoxMUNINSERT") as TextBox;
                    SqlParameter newMunData = new SqlParameter("@Name", SqlDbType.VarChar, 50);

                    newMunData.Direction = ParameterDirection.Input;

                    newMunData.Value = Name.Text;

                    insertParameters.Add(newMunData);
                    SqlDataSourceMUNICIPALITY.Insert();
                }
               

                if (e.CommandName == "Edit")  // MY PROBLEM AREA
                {
                    // Want to grab the value of TextBox1, store it for use in updatecommand  as WHERE condition value below
                }
              
               
                if (e.CommandName == "Update") // PENDING SUCCESS IN CONDITION ABOVE ... SHOULD WORK IF ABLE TO STORE VALUE IN ABOVE...RIGHT?
                {
                    TextBox Name = GridViewMUNICIPALITY.FooterRow.FindControl("TextBox1") as TextBox;
                    SqlDataSource3.UpdateCommand = "UPDATE [PROPERTIES] SET [Municipality] = " + Name.Text + " where [Municipality] = " + Label12.Text;
                }
            }

     

     

    Any direction would be appreciated..thanks

     

  • Re: Retrieving GridView nested EditItemTemplate TextBox control value

    10-05-2008, 12:45 PM
    • Loading...
    • Bohunkus
    • Joined on 05-22-2006, 10:13 PM
    • San Jose, Ca.
    • Posts 565
    • Points 2,552

      I'm not sure I understand. TextBox1 is in an Item row, but your Update code thinks it is in the Footer row. I think you need to following code to get to TextBox1 in the DataRow:

        protected void GridViewMUNICIPALITY_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            int index = Convert.ToInt32(e.CommandArgument);
    
            GridViewRow myRow = GridViewMUNICIPALITY.Rows[index];
            // then access the cell where the TextBox is.
        }

    HTHs. Got to run for now, will check back late today.

    Cheers,
    Chris

    Remember, you can mark more than 1 post as the answer.
  • Re: Retrieving GridView nested EditItemTemplate TextBox control value

    10-05-2008, 1:20 PM
    • Loading...
    • oldbcells
    • Joined on 09-18-2008, 1:33 AM
    • Posts 6
    • Points 1

     Throws a fit at:

    int index = Convert.ToInt32(e.CommandArgument);

    In Detail:

    System.FormatException was unhandled by user code
      Message="Input string was not in a correct format."
      Source="mscorlib"
      StackTrace:
           at System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal)
           at System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info)
           at System.String.System.IConvertible.ToInt32(IFormatProvider provider)
           at System.Convert.ToInt32(Object value)
           at DotNetNuke.Modules.pulladata2.altQuery.GridViewMUNICIPALITY_OnRowCommand(Object sender, GridViewCommandEventArgs e) in c:\DotNetNuke\DesktopModules\pulladata2\altQuery.ascx.cs:line 189
           at System.Web.UI.WebControls.GridView.OnRowCommand(GridViewCommandEventArgs e)
           at System.Web.UI.WebControls.GridView.HandleEvent(EventArgs e, Boolean causesValidation, String validationGroup)
           at System.Web.UI.WebControls.GridView.OnBubbleEvent(Object source, EventArgs e)
           at System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args)
           at System.Web.UI.WebControls.GridViewRow.OnBubbleEvent(Object source, EventArgs e)
           at System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args)
           at System.Web.UI.WebControls.LinkButton.OnCommand(CommandEventArgs e)
           at System.Web.UI.WebControls.LinkButton.RaisePostBackEvent(String eventArgument)
           at System.Web.UI.WebControls.LinkButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
           at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
           at System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
           at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
      InnerException:

  • Re: Retrieving GridView nested EditItemTemplate TextBox control value

    10-06-2008, 1:33 PM
    • Loading...
    • Bohunkus
    • Joined on 05-22-2006, 10:13 PM
    • San Jose, Ca.
    • Posts 565
    • Points 2,552

      I'm sorry, I'm not as familiar with OnRowCommand. Since you have Insert working, I would use the Edit and Delete buttons provided with the Gridview rather than making your own using RowCommand. Then you can use RowUpdating to catch the values before they are sent to the database. Then you would use something like:

    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) 
    { 
      TextBox txtName = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtName"); 
      // save the value to anywhere you want to
    } 

    Note: The Name column in this case is a template field with the TextBox ID value set to "txtName."  HTHs

    Cheers,
    Chris

    Remember, you can mark more than 1 post as the answer.
  • Re: Retrieving GridView nested EditItemTemplate TextBox control value

    10-08-2008, 4:28 AM
    Answer

    Hi oldbcells,

    You need to set CommandArgument='<%# Eval("Name") %>' in LinkButton "update".

    So in RowCommand event:

    if (e.CommandName == "Update") 
    {
        SqlDataSource3.UpdateCommand = "UPDATE [PROPERTIES] SET [Municipality] = " + e.CommandArgument.ToString() + " where [Municipality] = " + Label12.Text;
    }

    Thanks,

    Qin Dian Tang
    Microsoft Online Community Support

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Page 1 of 1 (7 items)