ASP.NET 2 Gridviews share the same OnSelectedIndexChanged event - asp.net

I have 2 Gridviews which share the same OnSelectedIndexChanged event.
How do I get the incoming Gridview the one which fired so that I can pass that GridView to DetailsView. In that DetailsView I need to access the selected Gridview columns.
Thanks In Advance.

first parameter is "sender" as an object and you can cast it to a gridview object than check its ID.
GridView grd = (GridView) sender;

The "sender" parameter of the OnSelectedIndexChanged event should be the GridView that the event came from. You can get at it like this:
public void MyGrid_OnSelectedIndexChanged(object sender, EventArgs e)
{
GridView grid = sender as GridView;
if (grid != null)
{
// Do something with grid
}
}

Related

How can I get selected item in DropDownList ASP control in Page_Load method?

I have DropDownList inside my ASP page.
When selection changed postback accured and the Page_Load method fired.
I need to get selected item(selectedValue and selectedIndex) in Page_Load method.
I know that I can use selectedIndexChanged event handler, but in my case it is not
suitable solution because of incorrect architecture.
Any idea how to get selected item in DropDownList control in Page_Load method.
With incorrect architecture is it useful to write
var selectedValue = Request.Params[drpDownList.UniqueID];
You should be OK as long as you check for postback -
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
var index = ddlDropDown.SelectedIndex;
// do stuff
}
}
I'm assuming that the control isn't dynamically created.

Non Gridview event capture edit mode row index

How can I get the index of a gridview row in edit mode from an event that is not a gridview event?
I have a DDL in a cell of the Gridview. When the DDL changes I want to perform an update to a label of another cell in the same row.
So I am using the DDL_SelectedIndexChanged Event and have everything working including the post to the label, but its always the frist row. What is the correct way to get the row index in edit mode from outside of a gridview event?
Thanks,
try this
protected void DDL_SelectedIndexChanged(Object sender, EventArgs e)
{
DropDownList ddlcontrol = (DropDownList) sender;
GridViewRow grrow = (GridViewRow) ddlcontrol.NamingContainer;
int rowindex = grrow.RowIndex;
}

Hide databound RadGrid rows

I have a telerik radgrid in my .aspx page name rgLowRise, and I have an ObjectDataSource as the DataSource set like rgLowRise.DataSourceID = odsLowRise. This works fine, but I want it not to show any records at first load, how can I do that? And I have to use DataSourceID and not DataSource due to some reasons.
Thanks
You can remove the DataSourceID from the markup or your code behind (Page_Load) and set at a later stage, e.g. Button_Click or some other event
protected void Button_Click(object sender, EventArgs e)
{
rgLowRise.DataSourceID = odsLowRise; //until you click this button, the grid would display nothing
}

dynamic columns disappears after postback

I have a GridView with some BoundFields and two TemplateFields. In these two TemplateFields, I dynamically create UserControls containing a DropDownList and a TextBox, which users can modify.
When I try to get the values of the controls after a PostBack, the values in BoundFields are still there but my dynamic controls disappears. I can create them again but it won't get the user's values... How can I get these values before they're lost?
Here's some of my code:
In the RowDataBound event:
Select Case type
Case "BooleanBis"
e.Row.Cells(2).Controls.Clear()
Dim list1 As BooleanBisList = New BooleanBisList(avant, False)
e.Row.Cells(2).Controls.Add(list1)
e.Row.Cells(4).Controls.Clear()
Dim list2 As BooleanBisList = New BooleanBisList(apres, True)
e.Row.Cells(4).Controls.Add(list2)
Case "Boolean"
e.Row.Cells(2).Controls.Clear()
Dim list3 As BooleanList = New BooleanList(avant, False)
e.Row.Cells(2).Controls.Add(list3)
e.Row.Cells(4).Controls.Clear()
Dim list4 As BooleanList = New BooleanList(apres, True)
e.Row.Cells(4).Controls.Add(list4)
End Select
In my button click event, I try to get the user control :
Case "String"
temp.ChampValeurApres = DirectCast(Tableau1.Rows(i).Cells(selectedColumn).Controls(1), TextBox).Text
but i get the error that it doesn't exist.
You should create dynamic controls in RowCreated instead of RowDataBound because this event gets fired on every postback whereas RowDataBound only will fire when the GridView gets databound to it's DataSource.
Dynamically created controls must be recreated on every postback with the same ID as before, then they retain their values in the ViewState and events will fire correctly(f.e. a DropDownList's SelectedIndexChanged event).
So you should create them in RowCreated and "fill" them in RowDataBound(f.e. the DropDownList datasource/Items or a TextBox-Text).
I had been using:
EnableViewState="false"
in the GridView attributes. Removing it solved my problem!
I just did
protected void Page_Load(object sender, EventArgs e)
{
if (!(Page.IsPostBack))
{
// Put the selected items which u want to keep on postback
}
else
{
//regenerate auto created controls
}
}
and it worked as well

ASP.NET Gridview assign EventHandler to child control

I have a GridView which is databound to an XML Datasource. For one of the columns I'm using a TemplateField and within it's ItemTemplate I have a CheckBox. I need to programatically add an EventHandler to the CheckBox. I was wondering if anyone can tell me which EventHandler from the GridView to use to add a CheckedChanged EventHandler to the CheckBox?
I've tried RowCreated and DataBound and haven't been able to get the CheckBox to postback with the CheckChanged EventHandler.
void gridPartnerSelection_RowCreated(object sender, GridViewRowEventArgs e)
{
GridViewRow row = e.Row;
if (row.RowType == DataControlRowType.DataRow)
{
CheckBox ckbSendEmail = row.Cells[2].FindControl("ckbSendEmail") as CheckBox;
ckbSendEmail.CheckedChanged += new EventHandler(ckbSendEmail_CheckedChanged);
}
}
Thank you.
Turns out I had to set the AutoPostBack property of the CheckBox to True :)
I'm willing to delete this question if it won't be helpful to anyone else.

Resources