Gridview row data disappears on new button click

Last post 12-15-2007 1:30 AM by sameer_khanjit. 3 replies.

Sort Posts:

  • Gridview row data disappears on new button click

    12-14-2007, 1:26 AM
    • Member
      26 point Member
    • S_a_r_a_h
    • Member since 10-29-2007, 11:15 AM
    • Posts 54

    Hi all....

    Im using ajax for the first time. plz help me out tin this scenario:

    I have gridview in the update panel. A link button is placed in a template feild of the gridview which adds new row on click.

    Prob is tht when i fill out the txtboxes and checkboxes of the first row and click the new row button....a new row appears but the previous row data is refereshed and all values disappear.

    I want tht when i click new row ...a new row is added and the previous row data stays in the textboxes of that row.

    i also want to save the previous row data somewhere(some data structure...) when clicking for new row.

    plz help me !!

    thisi s the code im using...

    <asp:ScriptManager ID="ScriptManager1" runat="server">

    </asp:ScriptManager>

    </div>

    <asp:UpdatePanel ID="Grid_UpdatePanel" runat="server">

    <ContentTemplate>

    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnPreRender="GridView1_PreRender"

    BackColor="White" BorderColor="#DEDFDE" BorderStyle="None" BorderWidth="1px"

    CellPadding="4" ForeColor="Black" GridLines="Vertical">

    <Columns>

    <asp:TemplateField ShowHeader="False">

    <ItemTemplate>

    <asp:LinkButton ID="add_row_LinkButton" runat="server" CausesValidation="False" CommandName="Insert"

    OnClick="add_row_LinkButton_Click">New</asp:LinkButton>

    </ItemTemplate>

    </asp:TemplateField>

    <asp:TemplateField HeaderText="Name">

    <ItemTemplate>

    <asp:TextBox ID="col_nameTextBox" runat="server" BackColor="White"></asp:TextBox>

    </ItemTemplate>

    </asp:TemplateField>

    <asp:TemplateField HeaderText="Abbreviation">

    <ItemTemplate>

    <asp:TextBox ID="col_abbrvTextBox" runat="server" BackColor="White"></asp:TextBox>

    </ItemTemplate>

    </asp:TemplateField>

    <asp:TemplateField HeaderText="Type">

    <ItemTemplate>

    <asp:DropDownList ID="col_type_dd" runat="server" AutoPostBack="True" OnSelectedIndexChanged="col_type_dd_SelectedIndexChanged" BackColor="White">

    <asp:ListItem>Please Select..</asp:ListItem>

    <asp:ListItem>bit</asp:ListItem>

    <asp:ListItem>date</asp:ListItem>

    <asp:ListItem>decimal</asp:ListItem>

    <asp:ListItem>integer</asp:ListItem>

    <asp:ListItem>string</asp:ListItem>

    </asp:DropDownList>

    </ItemTemplate>

    </asp:TemplateField>

    </Columns>

    </asp:GridView>

    </ContentTemplate>

    </asp:UpdatePanel>

  • Re: Gridview row data disappears on new button click

    12-14-2007, 9:14 AM
    • Contributor
      5,590 point Contributor
    • deblendewim
    • Member since 12-20-2006, 4:32 PM
    • Antwerp, Belgium
    • Posts 951

    hey S_a_r_a_h,

    Please look at this thread, could this be the cause?: http://forums.asp.net/t/1185745.aspx

    It is the DataBinding that makes the values disappear.

     

     

    If that is not the case, could you provide me with a better, easier to implement code example?
    You gave code, but no code-behind etc. (Think yourself as a programmer who never saw the code before .... try to copy and paste this code into a new project, Does it work from the 1st time? I GUESS NOT!!!!)

     

    Kind regards,
    Wim
     

    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
  • Re: Gridview row data disappears on new button click

    12-15-2007, 12:23 AM
    • Member
      26 point Member
    • S_a_r_a_h
    • Member since 10-29-2007, 11:15 AM
    • Posts 54

    Thanks for the reply.

     Here you go......

    protected void Page_Load(object sender, EventArgs e)

    {

    if (!IsPostBack)

    {

    RowInsert();

    }

     

    }

    protected void GridView1_PreRender(object sender, EventArgs e) //for adding new button only once

    {

    for (int i = 0; i < GridView1.Rows.Count - 1; i++)

    {

    GridView1.Rows[i].Cells[0].Controls[1].Visible =
    false;

    }

    }

    protected void RowInsert()//for columns grid

    {

    DataTable dt = new DataTable();

    dt.Columns.Add("Name");

    dt.Columns.Add("Abbreviation");

    dt.Columns.Add("Type");

    dt.Columns.Add("Length/Scale");

    dt.Columns.Add("Null?");

    dt.Columns.Add("History?");dt.Columns.Add("Description");

    dt.Rows.Add(dt.NewRow());

    Session[
    "col_DT"] = dt;

    GridView1.DataSource = dt;

    GridView1.DataBind();

    }

    protected void add_row_LinkButton_Click(object sender, EventArgs e)

    {

    DataTable dt = (DataTable)Session["col_DT"];

    dt.Rows.Add(dt.NewRow());

    Session[
    "col_DT"] = dt;

    GridView1.DataSource = dt;

    GridView1.DataBind();

    }

    protected void col_type_dd_SelectedIndexChanged(object sender, EventArgs e) //disable column on dd selected index

    {

    string dd_value ="";

    TextBox txt = new TextBox();

    foreach (GridViewRow r in this.GridView1.Rows)

    {

    DropDownList col_type_dd = r.FindControl("col_type_dd") as DropDownList;

    dd_value = col_type_dd.SelectedItem.Text;

    txt = r.FindControl(
    "col_len_sc_TextBox") as TextBox;

    }

    txt.Enabled =
    true;if (dd_value == "string" || dd_value == "decimal")

    {

    txt.Enabled = true;

    txt.BackColor = System.Drawing.Color.White;

     

    }

    else

    {

    txt.Enabled =
    false;

    txt.Text = "";

    txt.BackColor = System.Drawing.Color.LightGray;

    }

    }

    The no. of columns here must not match with the html code i posted earlier...so dont get confused..i only intended to shrink the code.

  • Re: Gridview row data disappears on new button click

    12-15-2007, 1:30 AM
    Answer
    • Contributor
      3,091 point Contributor
    • sameer_khanjit
    • Member since 12-10-2007, 8:06 AM
    • Indore India
    • Posts 638

     first of all  add Previous rows in a data table  like as

    for (int indexInt = 0; indexInt < dgContacts.Items.Count; indexInt++)
            {
                if (indexInt < count)
                {
                    dtContacts.Rows[indexInt]["First"] = ((TextBox)(dgContacts.Items[indexInt].Cells[0].Controls[1])).Text;
                    dtContacts.Rows[indexInt]["Last"] = ((TextBox)(dgContacts.Items[indexInt].Cells[1].Controls[1])).Text;
                    dtContacts.Rows[indexInt]["Title"] = ((TextBox)(dgContacts.Items[indexInt].Cells[2].Controls[1])).Text;
                    dtContacts.Rows[indexInt]["Email"] = ((TextBox)(dgContacts.Items[indexInt].Cells[3].Controls[1])).Text;
                    dtContacts.Rows[indexInt]["Phone"] = ((TextBox)(dgContacts.Items[indexInt].Cells[4].Controls[1])).Text;
                    dtContacts.Rows[indexInt]["Mobile"] = ((TextBox)(dgContacts.Items[indexInt].Cells[5].Controls[1])).Text;
                    dtContacts.Rows[indexInt]["contact"] = ((CheckBox)(dgContacts.Items[indexInt].Cells[6].Controls[1])).Checked;
                }
            }

    then add new row if there is any data in text boxes and bind grid from that table on click of that new button  

    We Are Looking for .NET/PHP Projects

    Contact Details :-

     Email - sameer.khanjit@gmail.com

     Mobile no. : +91-9893795983

     View Blog

    linkedin Asp.net Group

    Don't forget to click “Mark as Answer” on the post that helped you
Page 1 of 1 (4 items)
Microsoft Communities