2009-10-02 161 views
1

工作在asp.net vs05.I我接受一个gridview并在RowDataBount下填充dropDownList。gridview DropDownList选择值问题

<asp:GridView ID="GridView3" runat="server" AutoGenerateColumns="False" DataKeyNames="StudentID" OnSelectedIndexChanged="GridView3_SelectedIndexChanged" OnRowDataBound="GridView3_RowDataBound"> 
      <Columns> 
       <asp:BoundField DataField="StudentID" HeaderText="StudentID" ReadOnly="True" SortExpression="StudentID" /> 
       <asp:BoundField DataField="StudentName" HeaderText="StudentName" /> 
       <asp:TemplateField HeaderText="DivisionName"> 
        <EditItemTemplate> 

        </EditItemTemplate> 
        <ItemTemplate> 
         <asp:DropDownList ID="DropDownList1" runat="server" Width="160px"> 
         </asp:DropDownList> 
        </ItemTemplate> 
       </asp:TemplateField> 
       <asp:ButtonField ButtonType="Button" CommandName="Select" HeaderText="Update" Text="Update" /> 
      </Columns> 
     </asp:GridView> 



DropDownList DropDownList1 = (DropDownList)e.Row.FindControl("DropDownList1"); 

      studentInfos ostudentInfos = new studentInfos(); 
      BOstudentInfo oBOstudentInfo = new BOstudentInfo(); 
      ostudentInfos = oBOstudentInfo.Gets();   
      DropDownList1.DataTextField = "StudentName"; 
      DropDownList1.DataValueField = "StudentName"; 
      DropDownList1.SelectedValue = "StudentName"; 

      foreach (studentInfo oItem in ostudentInfos) 
      { 
       DropDownList1.Items.Add(oItem.StudentName); 


      } 

然后使用SelectedIndexChanged事件

GridViewRow selectRow = GridView3.SelectedRow; 
     String ID = selectRow.Cells[0].Text; 
     String Name = selectRow.Cells[1].Text; 
     DropDownList ddl = (DropDownList) selectRow.FindControl("DropDownList1"); 

     string s = ddl.SelectedValue; 

在这种情况下,为什么我总是得到列表的selectedIndex = 0和的SelectedValue =第一要素。

回答

1

使用IsPostBack页面属性。

if(!IsPostBack){ 
    ..... 
foreach (studentInfo oItem in ostudentInfos){ 
    DropDownList1.Items.Add(oItem.StudentName); 
    } 
} 
+1

+1良好的渔获@adatapost。 – klabranche 2009-10-02 03:25:10

0
DropDownList ddl = (DropDownList)sender; 
GridViewRow row = (GridViewRow)ddl.NamingContainer; 
String id = row.Cells[0].Text; 
string requestid = ((Label)row.FindControl("Label1")).Text; 
string type = ddl.SelectedValue;