Referencing webcontrols in an UpdatePanel in EditItemTemplate of a Gridview - asp.net

I am getting null reference exception when trying to reference a web control which is within an AJAX UpdatePanel, which itself is within and EditItemTemplate of a Gridview. I am doing this in the RowDataBound event handler of the gridview as follows:
protected void gvIntakes_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DataRowView row = (DataRowView)e.Row.DataItem;
UpdatePanel up = (UpdatePanel)e.Row.FindControl("Status_UpdatePanel"); //**** up is showing null on debug
Panel pnlSelectStaff = (Panel) up.FindControl("pnlSelectStaff") ;
Panel pnlSchedule = (Panel)up.FindControl("pnlSchedule");

Related

How to find out rowindex when fire dropdown event inside gridview

I am using two dropdownlist in gridview. Total 5 to 7 rows in gridview.
Bind ProductId and product name in First Dropdown. when select product that time databound in second dropdown.
so how to findout row number when fire dropdown's event.
I assume the property you're looking for is GridViewRow.RowIndex. To get the reference to the row from the SelectedIndexChanged event from your DropDownLis(s), you can use it's NamingContainer property:
private void DropDownList1_SelectedIndexChanged(object sender, System.EventArgs e)
{
DropDownList ddl = (DropDownList)sender;
GridViewRow row = (GridViewRow)ddl.NamingContainer;
int rowIndex = row.RowIndex;
// if you want to get the reference to the other DropDown in this row
DropDownList ddl2 = (DropDownList)row.FindControl("DropDownList2");
}
You can handle SelectedIndexChanged event of DropDownList.
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
GridViewRow gvr = (GridViewRow)((DropDownList)sender).Parent.Parent;
string rowNumber= GridView1.DataKeys[gvr.RowIndex].Value.ToString();
}

How to get the value of selected row using radiobutton in gridview in a button click

i need to get the value of the selected row of radiobutton in the gridview. the selected value is assigned to a textbox. my code is
protected void lnBTNDone_Click(object sender, EventArgs e)
{
GridViewRow gvRow = grdEventDetails.SelectedRow;
txtEventId.Text = gvRow.Cells[0].Text;
}
the problem is the gvRow value is assigned as null .
the linkbutton is not inside gridview. the gridview n linkbutton and the textbox are inside the user controls.
You can always get the GridViewRow via the sender's NamingContainer:
protected void lnBTNDone_Click(object sender, EventArgs e)
{
LinkButton lnBTNDone = (LinkButton)sender;
GridViewRow row = (GridViewRow)lnBTNDone.NamingContainer;
txtEventId.Text = row.Cells[0].Text;
}
Assuming that the LinkButton is in the selected row.
Btw, i'm yet not sure why SelectedRow is null there, maybe because the GridView's SelectedIndexChanged event is triggered after the LinkButton's click event.
protected void lnBTNDone_Click(object sender, EventArgs e)
{
for (int i = 0; i < grdEventDetails.Rows.Count; i++)
{
RadioButton rb = (grdEventDetails.Rows[i].FindControl("grdRdo")) as RadioButton;
if (rb.Checked == true)
{
txtEventId.Text = grdEventDetails.Rows[i].Cells[1].Text;
}
}
}

Change Field to Checkbox inside GridView

I am trying to change fields to a Checkbox inside a GridView.
I currently create Grid Columns Dynamically based on a query and some of the columns I want to change to a checkbox so it can be checked/unchecked by the user. I know I cant do this via .aspx page using but I am trying to stay away from statically creating the fields.
Any help would be great.
Make use of GridView's RowDataBound Event. Thus you can add any control to your GridView.
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
CheckBox chk1 = new CheckBox();
chk1.ID = "chkbox1";
e.Row.Cells[0].Controls.Add(chk1);
}
}
Edit for Comment:
Once you passed the values from database to gridview (out of scope for this question), You can access the values using e.Row.Cells[i].Text where "i" is the row.
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
TextBox txt1 = new TextBox();
txt1.Text = e.Row.Cells[0].Text;
e.Row.Cells[0].Controls.Add(txt1);
}
}

RadioButtonList Inside Repeater OnSelectedIndexChanged Not Firing

I have a RadioButtonList inside a Repeater. I have AutoPostback set to "true" and the OnSelectedIndexChanged defined. When I selected a different radiobutton in my list the page does postback, but my defined OnSelectedIndexChanged event is not catching or firing. Not sure what I am missing. Here is my markup and codebehind:
Use the repeater's itemcreated event to bind your eventhandler:
protected void Repeater!_ItemCreated(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
{
e.item.FindControl("TaskRadioButtonList").SelectedIndexChanged += new EventHandler(TaskRadioButtonList_OnSelectedIndexChanged);
}
}

Setting selectedvalue for a dropdownlist in GridView

I have a dropdownlist in my Gridview and I am binding a datasource to the gridview.
Though all the records are displaying properly the dropdown value is not selected.
How do I set something like
<%# Bind("Country") %> for a dropdownlist in the Gridview in ASP.net.
Thanks
You can hook into the RowDataBound event for the grid view, find the control and set the value.
protected void gridview_RowDataBound(object sender, GridViewRowEventArgs e)
{
var dropdownList = e.Row.FindControl("YOUR_DROP_DOWN") as DropDownList;
dropdownList .SelectedIndex = SET_VALUE_HERE;
}
Setting DropDownList value from a Datasource should be like:
protected void gridview_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DropDownList ddlCountry = (DropDownList)e.Row.FindControl("ddlCountry");
ddlCountry.SelectedValue = DataBinder.Eval(e.Row.DataItem, "Country").ToString();
}
}

Resources