Dynamically added gridview's linkbutton not working - c#

I have a gridview that has dynamically created columns.
Right now all the data is showing in its respective place but I am unable to get my link button to work. (Gridview disappears)
Here is my backend code, the gridview used is just an empty gridview
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
TemplateField tfield = new TemplateField();
tfield.HeaderText = "View";
GridView1.Columns.Add(tfield);
}
GridView1.DataBind();
}
string selectedID = null;
private void BindGrid(List<string> SelectedInfo)
{
DataTable dt = new DataTable();
//Dynamically adding columns and setting first column added as ID
selectedID = SelectedInfo[0];
for (int i = 0; i < SelectedInfo.Count; i++)
{
dt.Columns.Add(new DataColumn(SelectedInfo[i], typeof(string)));
}
List<string[]> InfoList = getInfoList(SelectedInfo);
for (int i = 0; i < InfoList.Count; i++)
{
dt.Rows.Add(InfoList[i]);
}
GridView1.DataSource = dt;
GridView1.DataBind();
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{ //Adding link button to first column
LinkButton lnkView = new LinkButton();
lnkView.ID = "lnkView";
lnkView.Text = "View";
lnkView.Click += ViewDetails;
lnkView.CommandArgument = (e.Row.DataItem as DataRowView).Row[selectedID].ToString();
e.Row.Cells[0].Controls.Add(lnkView);
}
}
protected void ViewDetails(object sender, EventArgs e)
{
//Popup the selected row ID
LinkButton lnkView = (sender as LinkButton);
GridViewRow row = (lnkView.NamingContainer as GridViewRow);
string id = lnkView.CommandArgument;
ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Row ID" + id + "')", true);
}
protected void CheckBoxList2_SelectedIndexChanged(object sender, EventArgs e)
{
List<string> SelectedInfo = new List<string>();
for (int i = 0; i < CheckBoxList2.Items.Count; i++)
{
if (CheckBoxList2.Items[i].Selected)
{
SelectedInfo.Add(CheckBoxList2.Items[i].Text);
}
}
BindGrid(SelectedInfo);
}

If you need to create controls at runtime, you better use the RowCreated event:
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{ //Adding link button to first column
LinkButton lnkView = new LinkButton();
lnkView.ID = "lnkView";
lnkView.Text = "View";
lnkView.Click += ViewDetails;
lnkView.CommandArgument = (e.Row.DataItem as DataRowView).Row[selectedID].ToString();
e.Row.Cells[0].Controls.Add(lnkView);
}
}
Otherwise you have to bind your GridView on every postback inViewDetails the Page_Load event.
Anyway if your GridView disappears, it's just because you have to bind it again in the ViewDetails method.

I hope below code will help you.
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
TemplateField tfield = new TemplateField();
tfield.HeaderText = "View";
GridView1.Columns.Add(tfield);
}
this.BindGrid();
}
private void BindGrid()
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[2] { new DataColumn("Id", typeof(int)),
new DataColumn("Name", typeof(string)) });
dt.Rows.Add(1, "A");
dt.Rows.Add(2, "B");
GridView1.DataSource = dt;
GridView1.DataBind();
}
protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
LinkButton lnkView = new LinkButton();
lnkView.ID = "lnkView";
lnkView.Text = "View";
lnkView.Click += ViewDetails;
lnkView.CommandArgument = (e.Row.DataItem as DataRowView).Row["Id"].ToString();
e.Row.Cells[2].Controls.Add(lnkView);
}
}
protected void ViewDetails(object sender, EventArgs e)
{
LinkButton lnkView = (sender as LinkButton);
GridViewRow row = (lnkView.NamingContainer as GridViewRow);
string id = lnkView.CommandArgument;
ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Id: " + id + "')", true);
}

Related

How to do OnClick on selected item in "dynamic" ASP.NET DropDownList without using JavaScript

Currently, there is 2 values in my DropDownList which is viewProduct and editProduct. So when the user selects one of the value, it will direct user to the particular function in ASP.NET. I have tried OnSelectedIndexChanged, but it seems like not working on the dynamic DropDownList.
Here is my code:
ddlAction.ID = "ddlForAction";
ddlAction.CssClass = "ddlList";
ddlAction.CausesValidation = false;
ddlAction.AutoPostBack = true;
ddlAction.Items.Add(new ListItem("View","viewProduct"));
ddlAction.Items.Add(new ListItem("Edit", "editProduct"));
e.Row.Cells[5].Controls.Add(ddlAction);
if(ddlAction.SelectedValue == "editProduct")
{
editProduct();
}
else if(ddlAction.SelectedValue == "viewProduct")
{
retrieveProduct();
}
Any idea how to solve it without using JavaScript?
My entire code:
protected void Page_Load(object sender, EventArgs e)
{
BindGridView();
}
private void BindGridView()
{
MySqlConnection conn = new MySqlConnection(sqlConn);
string sql = "SELECT prodID, prodName, stockLevel, reorderLevel, unitPrice from Product";
MySqlCommand cmd = new MySqlCommand(sql, conn);
MySqlDataAdapter sda = new MySqlDataAdapter(cmd);
DataTable dt = new DataTable();
sda.Fill(dt);
productList.DataSource = dt;
productList.DataBind();
}
protected void GridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
TableCell tc = new TableCell();
DropDownList ddlAction = new DropDownList();
if (e.Row.RowType == DataControlRowType.Header)
{
e.Row.Cells[0].Text = "Prod ID";
e.Row.Cells[1].Text = "Product Name";
e.Row.Cells[2].Text = "Qty";
e.Row.Cells[3].Text = "Reorder Level";
e.Row.Cells[4].Text = "Price (RM)";
e.Row.Controls.Add(tc);
}
if (e.Row.RowType == DataControlRowType.DataRow)
{
var qty = Int16.Parse(e.Row.Cells[2].Text);
var reorder = Int16.Parse(e.Row.Cells[3].Text);
if (qty <= reorder)
{
e.Row.Cells[2].CssClass = "redCell";
e.Row.Cells[3].CssClass = "redCell";
}
e.Row.Controls.Add(tc);
ddlAction.ID = "ddlForAction";
ddlAction.CssClass = "ddlList";
ddlAction.CausesValidation = false;
ddlAction.AutoPostBack = true;
ddlAction.Items.Add(new ListItem("View","viewProduct"));
ddlAction.Items.Add(new ListItem("Edit", "editProduct"));
e.Row.Cells[5].Controls.Add(ddlAction);
ddlAction.SelectedIndexChanged += DDLAction_OnSelectedIndexChanged;
if (ddlAction.SelectedValue == "editProduct")
{
editProduct();
}
else if (ddlAction.SelectedValue == "viewProduct")
{
retrieveProduct();
}
}
}
You need to declare an event handler then assign it to the control before adding the control to the page.
Here is an example :
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
{
CreateControl();
}
}
protected void CreateControl()
{
var ddlAction = new DropDownList();
ddlAction.ID = "ddlForAction";
ddlAction.CssClass = "ddlList";
ddlAction.CausesValidation = false;
ddlAction.AutoPostBack = true;
ddlAction.Items.Add(new ListItem("View","viewProduct"));
ddlAction.Items.Add(new ListItem("Edit", "editProduct"));
ddlAction.SelectedIndexChanged += DDLAction_OnSelectedIndexChanged;
e.Row.Cells[5].Controls.Add(ddlAction);
}
protected void DDLAction_OnSelectedIndexChanged(object sender, EventArgs e)
{
var action = (DropDownList) sender;
var choice = action.SelectedValue?.Trim();
string script = null;
switch (choice)
{
case "viewProduct":
script = "alert('View page!');";
break;
case "editProduct":
script = "alert('Edit page!');";
break;
default:
script = "alert('Unmatched Value!')";
break;
}
ScriptManager.RegisterStartupScript(this , typeof(_Default) , "TestJS" , $"<script>{script}</script>" , false);
}
use ScriptManager.RegisterStartupScript when you want to work with JS scripts. Response.Write will not parse script blocks as an executable blocks. It will throw a parse error though (you could check the browser console.

Gridview row in editing mode when I press "Insert" button? asp.net C#

I have a gridview and some control below the griview. I insert values from "control" and press "Insert" button then the values are inserted into the gridview row. Here is the gridview image when I insert values in control
and press the "Insert" button
and when I click on the "edit" button in griview the values of the selected edit row are displayed below the "controls" but when I update values in the control and click again "insert" then the row automatically edits the data, but I don't want this, I want that when I click on "insert" again that the row should not display in edit mode, here is the image
I want that when I click on "insert" button that the row values are replaced with new values.
Here is my aspx.cs code:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.Add("ID", System.Type.GetType("System.Int32"));
dt.Columns.Add("Column Name", System.Type.GetType("System.String"));
dt.Columns.Add("Data Type", System.Type.GetType("System.String"));
dt.Columns.Add("Allow Null", System.Type.GetType("System.Boolean"));
dt.Columns.Add("Primary Key", System.Type.GetType("System.Boolean"));
Session["MyDataTable"] = dt;
}
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
DataTable t = (DataTable)Session["MyDataTable"];
DataRow row1 = t.NewRow();
row1["ID"] = t.Rows.Count + 1;
row1["Column Name"] = TextBox1.Text;
row1["Data Type"] = DropDownList1.Text;
row1["Allow Null"] = Null.Checked == true ? "true" : "false";
row1["Primary Key"] = Primary.Checked == true ? "true" : "false";
t.Rows.Add(row1);
Session["MyDataTable"] = t;
GridView2.DataSource = t;
GridView2.DataBind();
TextBox1.Text = String.Empty;
DropDownList1.Text = "Select DataType";
}
protected void GridView2_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
DataTable t = (DataTable)Session["MyDataTable"];
t.Rows.RemoveAt(e.RowIndex);
GridView2.DataSource = t;
GridView2.DataBind();
}
protected void GridView2_RowEditing(object sender, GridViewEditEventArgs e)
{
DataTable t = (DataTable)Session["MyDataTable"];
//GridView2.EditIndex = e.NewEditIndex;
// TextBox1.Enabled = true;
//GridViewRow row = GridView2.Rows[e.NewEditIndex];
TextBox1.Text = GridView2.Rows[e.NewEditIndex].Cells[2].Text.ToString();
DropDownList1.Text = GridView2.Rows[e.NewEditIndex].Cells[3].Text.ToString();
GridView2.DataSource = t;
GridView2.DataBind();
}
protected void GridView2_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
DataTable t = (DataTable)Session["MyDataTable"];
GridView2.EditIndex = -1;
GridViewRow row = GridView2.Rows[e.RowIndex];
GridView2.DataSource = t;
GridView2.DataBind();
}
public void cancel(object sender, GridViewCancelEditEventArgs e)
{
DataTable t = (DataTable)Session["MyDataTable"];
GridView2.EditIndex = -1;
GridViewRow row = GridView2.Rows[e.RowIndex];
GridView2.DataSource = t;
GridView2.DataBind();
}

How to edit the GridView

I want to edit the GridView without involving the database
protected void myGridView_RowUpdating(object sender,GridViewUpdateEventArgs e)
{
string reg = (myGridView.DataKeys[e.RowIndex].Value.ToString());
GridViewRow row = (GridViewRow)myGridView.Rows[e.RowIndex];
TextBox name = (TextBox)row.Cells[1].Controls[0];
TextBox email = (TextBox)row.Cells[2].Controls[0];
myGridView.EditIndex = -1;
BindGird();
}
this my bindgrid fucntion:
protected void BindGird()
{
DataTable dt = (DataTable)Session["myCart"];
this.myGridView.DataSource = dt; this.myGridView.DataBind();
}
and this is the row editing event
protected void myGridView_RowEditing(object sender, GridViewEditEventArgs e)
{
myGridView.EditIndex = e.NewEditIndex; BindGird();
}

Gridview Rowcommand fires only once - Where is the error?

My code:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
BindDetails();
}
public void BindDetails()
{
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("Name", typeof(string)));
dt.Columns.Add(new DataColumn("Designation", typeof(string)));
dt.Columns.Add(new DataColumn("Address", typeof(string)));
for (int i = 0; i < 10; i++)
{
DataRow dr = dt.NewRow();
if (i % 2 == 0)
{
dr["Name"] = "Ram";
dr["Designation"] = "Manager";
dr["Address"] = "Nerul";
}
else
{
dr["Name"] = "Shyam";
dr["Designation"] = "CEO";
dr["Address"] = "Vashi";
}
dt.Rows.Add(dr);
}
grdDetails.DataSource = dt;
grdDetails.DataBind();
}
protected void grdDetails_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Button btnEdit = (Button)e.Row.FindControl("btnEdit");
//btnEdit.Attributes.Add("onclick", Page.ClientScript.GetPostBackEventReference((Control)sender, "value$" + e.Row.RowIndex.ToString()));
e.Row.Attributes.Add("onclick", Page.ClientScript.GetPostBackEventReference((Control)sender, "value$" + e.Row.RowIndex.ToString()));
}
}
protected void grdDetails_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "value")
{
txtName.Text = grdDetails.Rows[Convert.ToInt32(e.CommandArgument)].Cells[0].Text;
txtDesignation.Text = grdDetails.Rows[Convert.ToInt32(e.CommandArgument)].Cells[1].Text;
txtAddress.Text = grdDetails.Rows[Convert.ToInt32(e.CommandArgument)].Cells[2].Text;
}
}
kindly give the solution.
If i remember correctly the AttributeCollection is not persisted in ViewState. Then you need to register the click event on every postback not only on databinding. Hence try to use RowCreated instead of RowDataBound.
protected void grdDetails_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Button btnEdit = (Button)e.Row.FindControl("btnEdit");
//btnEdit.Attributes.Add("onclick", Page.ClientScript.GetPostBackEventReference((Control)sender, "value$" + e.Row.RowIndex.ToString()));
e.Row.Attributes.Add("onclick", Page.ClientScript.GetPostBackEventReference((Control)sender, "value$" + e.Row.RowIndex.ToString()));
}
}

ASP .NET RowUpdating GridView Troubles

I'm having trouble with the RowUpdating Method. My GridView is connected to our local SQL Server, and I'm trying to update the data. Here is the code for the RowUpdating Method from MSDN.
protected void TaskGridView_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
//Retrieve the table from the session object.
DataTable dt = (DataTable)Session["TaskTable"];
//Update the values.
GridViewRow row = GridView1.Rows[e.RowIndex];
dt.Rows[row.DataItemIndex]["Id"] = ((TextBox)(row.Cells[1].Controls[0])).Text;
dt.Rows[row.DataItemIndex]["Description"] = ((TextBox)(row.Cells[2].Controls[0])).Text;
dt.Rows[row.DataItemIndex]["IsComplete"] = ((CheckBox)(row.Cells[3].Controls[0])).Checked;
//Reset the edit index.
GridView1.EditIndex = -1;
//Bind data to the GridView control.
BindData();
}
I get this error:
System.NullReferenceException: Object reference not set to an instance of an object.
try this code once.
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
int id = Int32.Parse(GridView1.DataKeys[e.RowIndex].Value.ToString());
TextBox tname = (TextBox)row.FindControl("nam");
TextBox tques = (TextBox)row.FindControl("que");
MySqlCommand cmd = new MySqlCommand("update exam set name1=#name,ques=#ques where id = #id", con);
cmd.Parameters.Add("#id", MySqlDbType.Int16).Value = id;
cmd.Parameters.Add("#name", MySqlDbType.VarChar, 30).Value = tname.Text.Trim();
cmd.Parameters.Add("#ques", MySqlDbType.VarChar,40).Value = tques.Text.Trim();
con.Open();
cmd.ExecuteNonQuery();
GridView1.EditIndex = -1;
bind();
}
Not all GridViewRow have a DataItem.
You should add a if block around your code and verify the row being updated is of type DataRow.
if (e.Row.RowType == DataControlRowType.DataRow)
{
your code here...
}
More regarding RowTypes : http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridviewrow.rowtype.aspx
public void bindGvEdit()
{
GridView1.DataSource = obj1.SelectAlltbl();
GridView1.DataBind();
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
bindGvEdit();
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
bindGvEdit();
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
GridViewRow row = GridView1.Rows[e.RowIndex];
obj1.Id = Convert.ToInt32(GridView1.DataKeys[row.RowIndex].Value);
obj1.Name = ((TextBox)row.Cells[1].Controls[1]).Text;
obj1.Description = ((TextBox)row.Cells[2].Controls[1]).Text;
obj1.Updatetbl();
GridView1.EditIndex = -1;
bindGvEdit();
}

Categories