Hello Mr. Norway,
I'm kind of confused about what you are lookiing for. I've put some code here and if you could explain what values you are looking for (paste some code yourself) that would be a help.
<%
@.PageLanguage="C#" %><!
DOCTYPEhtmlPUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><
scriptrunat="server">protectedvoid GridView1_SelectedIndexChanged(object sender,EventArgs e){
string str1 = GridView1.SelectedValue.ToString();// are you talking about getting data here?}
protectedvoid DropDownList1_SelectedIndexChanged(object sender,EventArgs e)
{
// are you talking about getting data here?}
</
script><
htmlxmlns="http://www.w3.org/1999/xhtml"><
headrunat="server"><title>Untitled Page</title></
head><
body><formid="form1"runat="server">
<div><asp:GridViewID="GridView1"runat="server"AutoGenerateColumns="False"DataKeyNames="Id"DataSourceID="ObjectDataSource1"OnSelectedIndexChanged="GridView1_SelectedIndexChanged"><Columns><asp:BoundFieldDataField="Id"HeaderText="Id"ReadOnly="True"SortExpression="Id"/><asp:TemplateFieldHeaderText="Email"SortExpression="Email"><EditItemTemplate><asp:DropDownListID="DropDownListName"OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"runat="server"AutoPostBack="true"><asp:ListItem>Peter</asp:ListItem><asp:ListItem>Tom</asp:ListItem><asp:ListItem>Tammy</asp:ListItem></asp:DropDownList><asp:TextBoxID="TextBox1"runat="server"Text='<%# Bind("Email") %>'></asp:TextBox></EditItemTemplate><ItemTemplate><asp:LabelID="Label1"runat="server"Text='<%# Bind("Email") %>'></asp:Label></ItemTemplate></asp:TemplateField><asp:BoundFieldDataField="Name"HeaderText="Name"SortExpression="Name"/></Columns></asp:GridView><asp:ObjectDataSourceID="ObjectDataSource1"runat="server"DeleteMethod="Delete"InsertMethod="Insert"OldValuesParameterFormatString="original_{0}"SelectMethod="GetMembers"TypeName="BO1"UpdateMethod="Update"><DeleteParameters><asp:ParameterName="Id"Type="Int32"/></DeleteParameters><UpdateParameters><asp:ParameterName="Id"Type="Int32"/><asp:ParameterName="Name"Type="String"/><asp:ParameterName="Email"Type="String"/></UpdateParameters><InsertParameters><asp:ParameterName="Id"Type="Int32"/><asp:ParameterName="Name"Type="String"/><asp:ParameterName="Email"Type="String"/></InsertParameters></asp:ObjectDataSource></div></form>
</
body></
html>I'm assuming you have a DropDownList in each row of the GridView. If so, try something like this (if it's only in one row there are probably easier ways):
protected void MyDropDownList_SelectedIndexChanged(object sender, EventArgs e){DropDownList MyDropDownList = (DropDownList)sender;GridViewRow gvr = (GridViewRow)MyDropDownList.NamingContainer;int selectedRow = gvr.RowIndex;} Hope that helps.
Aaron
Tnx Aaron!
Thats exactly what i was looking for!
Greetings from Norway! :D
Hi AGolden,
Great solution! I didn't realize you could do this. NamingContainer slipped by me. So, I started playing with it some more and was surprised that I can not get the bound data from the GridViewRow. What Am I missing? here is the one line I added (after your code).
Thanks for the great post.
protectedvoid DropDownList1_SelectedIndexChanged(object sender,EventArgs e){
DropDownList MyDropDownList = (DropDownList)sender;GridViewRow gvr = (GridViewRow)MyDropDownList.NamingContainer;int selectedRow = gvr.RowIndex;
// why does this not get my bound dataitem? It gets null instead?BusinessObjectItem boi = (BusinessObjectItem)gvr.DataItem;
}
Thanks Peter. I believe the DataItem property is only not null when DataBinding (e.g. so you could use it during OnRowDataBound), but not in a PostBack. I did a couple of tests and it looks like by the time you get to GridView.DataBound, it's null.
No comments:
Post a Comment