You must Sign In to post a response.
  • Category: ASP.NET

    Loading grid view when button in datalist is clicked (inside item template)

    i am having a grid view and a datalist view .both are under update panel. In datalist i am having a button in itemtemplate which is having follwing code and i have to populate gridview with same values like in datalist.

    <itemtemplate>
    <asp:Button ID="testing" CausesValidation="false" runat="server" OnClientClick='<%#String.Format("return SetItemDetails({0},'{1}','{2}','{3}','{4}')", Eval("ItemId"), Eval("ItemName"),Eval("Cost"),Eval("Dateofexpire"),Eval("Quantity"), Eval("Units"),Eval("UnitPrice"),Eval("Location"),Eval("Description")) %>' OnClick="testing_Click" Height="35px" Width="100%" Text='<%#"("+ DataBinder.Eval(Container.DataItem, "ItemName")+")"+" (Price:"+ DataBinder.Eval(Container.DataItem, "UnitPrice")+")"+" (Qty:"+ DataBinder.Eval(Container.DataItem, "Quantity")+")"+" (units:"+ DataBinder.Eval(Container.DataItem,"Units")+")"+" (Location:" + DataBinder.Eval(Container.DataItem,"Location")%>'
    CommandArgument='<%#Eval("ItemId") %>' ToolTip ='<%#"Qty:" +"("+ DataBinder.Eval(Container.DataItem, "Quantity")+")"+ " Location:" + DataBinder.Eval(Container.DataItem,"location")+ " Units:" + DataBinder.Eval(Container.DataItem,"Units")%>'/>


    What I have tried:

    i have working with itemcommand with command argument but gridview is not loading..
  • #768849
    Hi,

    As per my understanding you want to bind your gridview while click the button inside datalist, it doesn't mean the nested gridview concept. What you want to do means first bind your datalist on page load, and while click the button you have to trigger datalist item command event for binding gridview.

    Ex:


    protected void DataList1_ItemCommand(object source,
    DataListCommandEventArgs e)
    {
    if (e.CommandName == "Add")
    {
    // perform your action for binding gridview
    }
    }


    this is example you have to bind grid based on parent id of datalist you have to pass the id of the parent record and bind the gridview based on parent id.

    Hope this helps you....

    --------------------------------------------------------------------------------
    Give respect to your work, Instead of trying to impress your boss.

    N@veen
    Blog : http://naveens-dotnet.blogspot.in/

  • #768875
    thanks for the response ...i got it the other way

  • #768877
    Hi Mohammed,

    Nice to hear that, if possible please share the way you are approaching that's helpful to someone else who are looking for the same.

    --------------------------------------------------------------------------------
    Give respect to your work, Instead of trying to impress your boss.

    N@veen
    Blog : http://naveens-dotnet.blogspot.in/

  • #768879
    i have done it by sending the command argument (itemid) to database and brought values from there through datareader.
    but i believe that is not a precise way to do even i got the answer.

  • #768886
    post your code behind code so that we can help you better. I think you have not bind that gridview after assigning datasource
    Please share code with us.

    Thanks
    Koolprasd2003
    Editor, DotNetSpider MVM
    Microsoft MVP 2014 [ASP.NET/IIS]

  • #768891
    --->>>Code View
    protected void datalist_ItemCommand(object source, DataListCommandEventArgs e)
    {
    //create a datatable,get the values from database and bind the datatable with gridview
    if (ViewState["mytable"] == null) // checking if table is already present or it is null using viewstate
    {
    dt = new DataTable("mytable");
    dt.Columns.Add("ItemId", typeof(int));
    dt.PrimaryKey = new DataColumn[] { dt.Columns["ItemId"] }; //primaryt key to itemid column
    dt.Columns.Add("ItemName", typeof(string));
    dt.Columns.Add("Cost", typeof(string));
    dt.Columns.Add("Dateofexpire", typeof(string));
    dt.Columns.Add("Quantity", typeof(string));
    dt.Columns.Add("Units", typeof(string));
    dt.Columns.Add("UnitPrice", typeof(string));
    dt.Columns.Add("Location", typeof(string));
    dt.Columns.Add("Description", typeof(string));
    dt.Columns.Add("Discount", typeof(string));
    dt.Columns.Add("Value", typeof(string));
    }
    else
    {
    //If yes then get it from current session
    dt = (DataTable)ViewState["mytable"];
    }
    int empid = Convert.ToInt32(e.CommandArgument.ToString()); //getting item id
    strSqlCommmand = "Select * from ItemsMaster Where ItemId=@ItemId";
    cn = new SqlConnection("Data Source=USER;User Id=sa;Password=123456;Database=BillData");
    if (cn.State != ConnectionState.Open)
    cn.Open();
    cmd = new SqlCommand(strSqlCommmand, cn);
    cmd.Parameters.AddWithValue("ItemId", empid);
    SqlDataReader dr = cmd.ExecuteReader();
    try {
    if (dr.Read())
    {

    int itemid = Convert.ToInt32(dr["ItemId"].ToString());
    string itemname = dr["ItemName"].ToString();
    string cost = dr["Cost"].ToString();
    string datime = dr["Dateofexpire"].ToString();
    string qlty = dr["Quantity"].ToString();
    string units = dr["Units"].ToString();
    string unitprice = dr["UnitPrice"].ToString();
    string loctn = dr["Location"].ToString();
    string desc = dr["Description"].ToString();
    }

    DataRow nw = dt.NewRow();
    nw["ItemId"] = itemid.ToString();
    nw["ItemName"] = itemname.ToString();
    nw["Cost"] = cost.ToString();
    nw["Dateofexpire"] = datime.ToString();
    nw["Quantity"] = qlty.ToString();
    nw["Units"] = units.ToString();
    nw["UnitPrice"] = unitprice.ToString();
    nw["Location"] = loctn.ToString();
    nw["Description"] = desc.ToString();
    nw["Discount"] = "";
    nw["Value"] = "";
    dt.Rows.Add(nw);
    ViewState["mytable"] = dt;
    BillingGridView.DataSource = dt;
    BillingGridView.DataBind();
    dr.Close();
    cn.Close();
    }

    }
    }
    catch (Exception ex)
    {
    Response.Write(ex.Message);
    }

    }

  • #768933
    This example of code snippet shows on how to generate a Row in GridView with TextBoxes when the click

    private void AddNewRowToGrid()
    {
    int rowIndex = 0;

    if (ViewState["CurrentTable"] != null)
    {
    DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
    DataRow drCurrentRow = null;
    if (dtCurrentTable.Rows.Count > 0)
    {
    for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
    {
    TextBox box1 = (TextBox)Gridview1.Rows[rowIndex].Cells[1].FindControl("TextBox1");
    TextBox box2 = (TextBox)Gridview1.Rows[rowIndex].Cells[2].FindControl("TextBox2");

    drCurrentRow = dtCurrentTable.NewRow();
    drCurrentRow["RowNumber"] = i + 1;

    dtCurrentTable.Rows[i - 1]["Column1"] = box1.Text;
    dtCurrentTable.Rows[i - 1]["Column2"] = box2.Text;
    rowIndex++;
    }
    dtCurrentTable.Rows.Add(drCurrentRow);
    ViewState["CurrentTable"] = dtCurrentTable;

    Gridview1.DataSource = dtCurrentTable;
    Gridview1.DataBind();
    }
    }
    else
    {
    Response.Write("ViewState is null");
    }


  • Sign In to post your comments