今天遇到了在GridView中使用DropDownList的OnSelectedIndexChanged事件。在此小结下,方便博友们参考哈。
前台代码:
<asp:TemplateField HeaderText="分值" ItemStyle-HorizontalAlign="Center"> <ItemTemplate> <asp:DropDownList ID="GradeList" runat="server" Width="100px" OnSelectedIndexChanged="dd_SelectedIndexChanged" AutoPostBack="true"> <asp:ListItem></asp:ListItem> <asp:ListItem>0</asp:ListItem> <asp:ListItem>10</asp:ListItem> <asp:ListItem>20</asp:ListItem> <asp:ListItem>30</asp:ListItem> <asp:ListItem>40</asp:ListItem> <asp:ListItem>50</asp:ListItem> <asp:ListItem>60</asp:ListItem> <asp:ListItem>70</asp:ListItem> <asp:ListItem>80</asp:ListItem> <asp:ListItem>90</asp:ListItem> <asp:ListItem>100</asp:ListItem> </asp:DropDownList> </ItemTemplate> </asp:TemplateField>
后台OnSelectedIndexChanged事件:
/// <summary> /// 评分 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void dd_SelectedIndexChanged(object sender, EventArgs e) { GridViewRow gvr = (GridViewRow)((Control)sender).Parent.Parent; DropDownList ddl = (DropDownList)sender;//获得GridView1中的DropDownList1控件 //也可以这样写 //GridViewRow row = (GridViewRow)ddl.Parent.Parent; //GridViewRow row1 = (GridViewRow)ddl.NamingContainer;//通过ddl控件找到所在的行 //int aa = Convert.ToInt32(row1.RowIndex.ToString());//获得行的rowindex QueryBuilder qb = new QueryBuilder(); qb.AddFilter("MIS_GradeInfo.SF_UserID", WebCommon.SQL_EQUAL, gvr.Cells[0].Text); qb.AddFilter("MIS_GradeInfo.DF_Week", WebCommon.SQL_EQUAL, gvr.Cells[2].Text); IList<MIS_GradeInfoModel> MIS_GradeInfoModel = bll.GetMIS_GradeInfoModelByCondition(qb); if (MIS_GradeInfoModel.Count > 0) { model = MIS_GradeInfoModel[0]; model.DF_UserID = UserNum.Value; model.DF_UserName = UserName.Text; model.GradeNum = ddl.SelectedValue.ToString();//获得选择的值 model.DF_Flag = 1;//表示打分标识 model.DF_Date = DateTime.Now.ToShortDateString(); model.Up_DF_Date = DateTime.Now.ToShortDateString(); bll.Update_MIS_GradeInfo(model); } }