Checking or unchecking a gridview checkbox dynamically - c#

I have two gridviews, one is on the main page and the other is modal. Both gridviews have one checkbox defined as asp:TemplateField.
What I want is: When I click one checkbox on the modal gridview, all the related checkboxes in the other gridview checked. The code is as follows:
protected void Secil_CheckedChanged(object sender, EventArgs e)
{
int selRowIndex = ((GridViewRow)(((CheckBox)sender).Parent.Parent)).RowIndex;
CheckBox cb = (CheckBox)GridIller.Rows[selRowIndex].FindControl("Secil");
if (cb.Checked)
{
string secili = HttpUtility.HtmlDecode(GridIller.Rows[selRowIndex].Cells[1].Text);
foreach (GridViewRow row in GridView1.Rows)
{
CheckBox cb1 = (CheckBox)row.FindControl("Sec");
if (HttpUtility.HtmlDecode(row.Cells[7].Text) == secili)
{
if (row.RowType == DataControlRowType.DataRow)
{
GridView1.EditIndex = -1;
cb1.Checked = true;
}
}
}
}
When I debug the program I see that cb1.checked=true; statement is executing but checkbox in the gridview remains unchecked.

Related

Why condition in rowdatabound is getting failed?

I am trying to show a button where IsPublished is true, it works but except for the first row in the grid. Why? I have been trying it for so long but it doesn't work at all
protected void gvNITs_RowDataBound(object sender, GridViewRowEventArgs e)
{
try
{
//LinkButton lb = e.Row.FindControl("btnLinkDownload") as LinkButton;
//if (lb != null)
// ScriptManager.GetCurrent(this).RegisterPostBackControl(lb);
if (e.Row.RowType == DataControlRowType.DataRow)
{
LinkButton Lbtn_change = (LinkButton)e.Row.FindControl("Lbtn_change");
HiddenField hdnPublishNITDate = e.Row.FindControl("hdnPublishedNITdate") as HiddenField;
DateTime? dtPublishedNITDate = string.IsNullOrEmpty(hdnPublishNITDate.Value) == true ? null : (DateTime?)hdnPublishNITDate.Value.ToDate();
HiddenField hdnIsPublishedNIT = e.Row.FindControl("hdnIsPublishedNITs") as HiddenField;
bool IsPublished = hdnIsPublishedNIT.Value.ToBool();
GridView gv = (GridView)sender;
foreach (GridViewRow gvr in gv.Rows)
{
if (IsPublished == true)
{
Lbtn_change.Visible = true;
}
}
}
}
}
It does not work because you have a nested loop. The RowDataBound event is triggered when a row is being added to the GridView. But in the RowDataBound event you loop all the rows in the GridView foreach (GridViewRow gvr in gv.Rows)
If you check gv.Rows.Count you will find that it is 0 in the first row because it has not yet been added to the GridView.
But you do not need that loop anyway since you already have access to Lbtn_change. So set the Visible property without the loop.

GridView cells are empty during SelectedIndexChanged when adding a column dynamically.

I'm creating a GridView wrapper class which retrieves certain bits of data from the database and then displays it. When a row is selected then I raise an event which other classes will listen to. I have also made a method which adds a new column to the GridView which some simple icons in it.
Here is some of my code:
protected override void OnPreRender(EventArgs e)
{
CreateGridView(); // This creates the gridview and sets it properties
PopulateGridView(); // This queries the DB and binds the data
AddIconColumn(gvData.HeaderRow); //Add Icon Column to header
foreach (GridViewRow row in gvData.Rows)
{
AddIconColumn(row); // Add Icon Column to each row
}
}
protected virtual void AddIconColumn(GridViewRow row)
{
if (!ShowIcons) return;
if (row.RowType == DataControlRowType.Header)
{
TableHeaderCell HeaderCell = new TableHeaderCell();
HeaderCell.Text = string.Empty;
row.Cells.AddAt(index, HeaderCell);
}
if (row.RowType == DataControlRowType.DataRow)
{
var NewCell = new TableCell();
var SwitchCell = row.Cells[6] as TableCell;
NewCell.CssClass = columnValueDictionary[SwitchCell.Text];
row.Cells.AddAt(index, NewCell);
}
}
public void gvData_SelectedIndexChanged(object sender, EventArgs e)
{
GridViewRow row = gvData.SelectedRow; // Get the selected row
int selectedID;
var id = row.Cells[2].Text; // When the icon column is enabled, all the cells are empty at this point !
if (int.TryParse(id, out selectedObjID))
{
if (SelectedIDChanged != null)
{
SelectedIDChanged(this, new SelectedIDChangedEventArgs(selectedObjID));
}
}
}
When I add the new column with the icons, the GridView's SelectedIndexChanged method can't retrieve any data from the row. All the cells text is empty during the SelectedIndexChanged event. If I dont add the icon row (by setting ShowIcons to false) then the cells have the correct text during the SelectedIndexChanged changed event.
What could be causing this behaviour?

Event handler not firing on dynamic button click

I have a dynamically created button with an onclick event handler. The problem is that when I click the button it does not hit the event in the code-behind.
protected void gvOrder_RowDataBound(object sender, GridViewRowEventArgs e)
{
DataTable dt = ds.Tables[0];
DropDownList ddl = new DropDownList();
TextBox txt = new TextBox();
int index = 1;
if (e.Row.RowType == DataControlRowType.DataRow)
{
ddl = e.Row.FindControl("ddlNewO") as DropDownList;
txt = e.Row.FindControl("txtNewT") as TextBox;
}
foreach (DataRow r in dt.Rows)
{
string listitem = Convert.ToString(index);
ddl.Items.Add(listitem);
index++;
}
ddl.SelectedIndex = e.Row.RowIndex;
if (e.Row.RowIndex == 0)
{
ddl.Enabled = false;
txt.Enabled = false;
}
else if (e.Row.RowIndex != 0)
{
ddl.Items.Remove("1");
//Create ED button
if (e.Row.RowType == DataControlRowType.DataRow)
{
Button btnED = new Button();
btnED.ID = "btnED";
btnED.CssClass = "buttonsmall";
//btnED.CommandName = "ED";
btnED.EnableViewState = true;
btnED.Click += new EventHandler(btnED_Click);
foreach (DataRow r in dt.Rows)
{
btnED.Attributes.Add("ID", r.ItemArray[2].ToString());
if (r.ItemArray[3].ToString() == "1")
{
btnED.Text = "Disable";
}
else
{
btnED.Text = "Enable";
}
//Add button to grid
e.Row.Cells[5].Controls.Add(btnED);
}
}
}
}
protected void btnED_Click(object sender, EventArgs e)
{
// Coding to click event
}
So the problem here is that when the page is being recreated on post back - there is no more button! Dynamic controls need to be added on the page on every post back to fire events properly. In your case however on the first load when the GridView is binding you add the button to the page. But on the post back after the click the button is not added again, because GridView is not data bound again. Therefore ASP.NET cannot derive the source of the event, and supresses it.
Fix here is to bind GridView with data on every post back. Literally if you had if (!IsPostBack) - remove it. Or you can add the button in the template field and play with visibility - may be an approach as well.
You need to add a click handler on Row Created not on Data Bound I believe.
protected void gvOrderRowCreated(object sender, GridViewRowEventArgs e)
{
switch (e.Row.RowType) {
case DataControlRowType.DataRow:
Button btn = (Button)e.Row.FindControl("btnED");
btn.Command += btnED_Click;
break;
}
}

passing parameter from gridview

trying to pass parameter from a label in gridview, only the label text from the first row are passed.
not sure what is missing.
protected void GV1_OnRowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "edit")
{
foreach (GridViewRow row in GridView1.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
Label lbl_taskID = (Label)row.FindControl("lbl_taskID");
Session["TaskID"] = lbl_taskID.Text;
Response.Redirect("~/tasks_edit.aspx");
}
}
}
}
You are breaking loop with Response.Redirect You need to put the do Response.Redirect out side loop to set all value, you also need to concatenate value of lbl_taskID all rows instead of over writting.
protected void GV1_OnRowCommand(object sender, GridViewCommandEventArgs e)
{
string taskIds = string.Empty;
if (e.CommandName == "edit")
{
foreach (GridViewRow row in GridView1.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
Label lbl_taskID = (Label)row.FindControl("lbl_taskID");
if(Session["TaskID"] != null)
taskIds = Session["TaskID"].ToString();
Session["TaskID"] = taskIds + lbl_taskID.Text + ",";
}
}
Response.Redirect("~/tasks_edit.aspx");
}
}
If only the first row is used, then why you using foreach loop? You can simply find the control of the 0th row.
if (e.CommandName == "edit") //this makes sure that you have a row
{
Label lbl_taskID = (Label)GridView1.Rows[0].FindControl("lbl_taskID");
Session["TaskID"] = lbl_taskID.Text;
Response.Redirect("~/tasks_edit.aspx");
}
If you mean label text from currently editing column then, take the index of the editing column from the CommandArgument and get the label
if (e.CommandName == "edit") //this makes sure that you have a row
{
int index = Convert.ToInt32(e.CommandArgument); //currently editing row index
Label lbl_taskID = (Label)GridView1.Rows[index].FindControl("lbl_taskID");
Session["TaskID"] = lbl_taskID.Text;
Response.Redirect("~/tasks_edit.aspx");
}
How come you have lbl_taskID in every row? In your foreach loop your doing-
Label lbl_taskID = (Label)row.FindControl("lbl_taskID");
You are actually only taking the value of lbl_taskID present in your first row. The next rows will not have the same element again. Your coding is wrong. You will need to name your labels in each row with some thing like this- label0, label1,... then in your code you can do-
int i=0;
foreach(GridViewRow row in GridView1.Rows)
{
Label xyz = (Label)row.FindControl("Label"+i);
Session["TaskID"+i] =xyz.Text; //to have unique session variables
i++;
}
Response.Redirect("~/tasks_edit.aspx"); // you should redirect only when you come out of the loop
You are using a foreach loop, but you are using the loop to assign a single value to a single Session variable. So what do you expect?
However, i would assume that your last row is assigned not the first.
You need to put Response.Redirect("~/tasks_edit.aspx") outside of the loop since it will abort the current request. You might want to assign the row which is currently in edit-mode:
foreach (GridViewRow row in gridView1.Rows)
{
if (row.RowState == DataControlRowState.Edit)
{
Label lbl_taskID = (Label)row.FindControl("lbl_taskID");
Session["TaskID"] = lbl_taskID.Text;
break;
}
}
Response.Redirect("~/tasks_edit.aspx");
Side-note: you don't need to check the DataControlRowType since GridView.Rows only returns rows with DataControlRowType.DataRow anyway(unlike RowDataBound-event).
Edit: Instead of RowCommand i would use the click event of the LinkButton:
protected void EditLink_Clicked(Object sender, EventArgs e)
{
// get the LinkButton reference
LinkButton link = (LinkButton) sender;
// get the GridViewRow reference via NamingContainer
GridViewRow row = (GridViewRow) link.NamingContainer;
Label lbl_taskID = (Label) row.FindControl("lbl_taskID");
Session["TaskID"] = lbl_taskID.Text;
Response.Redirect("~/tasks_edit.aspx");
}
I have used the auto generated EDIT button from gridview wizard and it is working.
sorry for your time waste.

I need to copy the text from a gridview texbox to the others in the column

This is my code in c#
protected void ibtCopiar_Click(object sender, ImageClickEventArgs e)
{
GridViewRow grdrows = DGPlanilla.Rows[0];
TextBox tb1 = (TextBox)grdrows.FindControl("TextBox1");
string valor = tb1.Text;
if (valor != null || valor != "0")
{
foreach (GridViewRow grdrow in DGPlanilla.Rows)
{
grdrow.Cells[5].Text = valor;
}
}
}
this my code for the button
when i debug i see that the value i have in the firts box is pass to the other textboxes in the column, but when it dysplay the page onle the first box show the value i have enter. the other texboxes donĀ“t change.
If the other cells contain textboxes as well, you will need to find them and set their text value.
foreach (GridViewRow grdrow in DGPlanilla.Rows)
{
TextBox toDisplay = (TextBox)grdrow.FindControl("TextBox1");
toDisplay.Text = valor;
}

Categories

Resources