The DropDownList control and the GridView control are quite common in Asp.net. The following are the explanations of the control, some of which are commonly used and some are occasionally used for searching, using, and recording.
General Usage of DropDownList:
1. Bind DropDownList to a simple data source
Write a simple data source for the time being to describe the effect.
private void BindDropDownUp() { ArrayList al = new ArrayList(); al.Add("11"); al.Add("22"); al.Add("33"); this.DropDownList1.DataSource = al; this.DropDownList1.DataBind(); }
Obtain the value selected in DropDownList: string text = this. DropDownList1.SelectedItem. Text;
2. DropDownList binding complex data sources
Extract a dataset ds from the database. A value is displayed in the text box of the DropDownList control. After selection, the bound value can be obtained in the background. The details are as follows:
Private void BindDropDownUp () {string strSql = "select * from [OSCE]. [dbo]. [QuestionType] "; DataSet ds = Query (strSql); this. dropDownList1.DataSource = ds; this. dropDownList1.DataTextField = "QT_Name"; this. dropDownList1.DataValueField = "QT_ID"; this. dropDownList1.DataBind (); // bind the data source to a similar (GridView) control}
Get the value of the text box of the DropDownList control: string text = this. DropDownList1.SelectedItem. Text;
Obtain the value bound to the DropDownList control: string text2 = this. DropDownList1.SelectedValue;
3. assign a value to DropDownList directly during page Initialization
ASIDE: this feature is widely used and easy to implement, provided that you know it. It took a long time to find out.
ListItem li = DropDownList1.Items. FindByText ("surgery"); // surgery is the value to be displayed, provided that the if extfield must contain if (li! = Null) {int index = DropDownList1.Items. IndexOf (li); DropDownList1.SelectedIndex = index ;}
General Usage of GridView
1. Code of the gridview Front-End Interface
There are two main ways to create a column in a gridview:
1) Data Binding indicates fields displayed as text in the data binding control. DataField = "AnswerNum", which is a field in the data source. Example:
<asp:BoundField DataField ="AnswerNum" > <ItemStyle Width ="8%" HorizontalAlign ="Center" /> </asp:BoundField>
2) create a template, for example:
<Asp: TemplateField HeaderText = "View"> <ItemTemplate> <asp: linkButton ID = "LinkButtonViewSOption" runat = "server" CommandName = "ViewSOption" CommandArgument = '<% # Bind ("QO_ID") %>'> description </asp: linkButton> </ItemTemplate> <ItemStyle Width = "5%" HorizontalAlign = "Center"/> </asp: TemplateField>
ItemStyle is its template style, which can be adjusted according to specific requirements.
2. Bind a data source
this.gvQuestions.DataSource = ExamQuestionInfoList; this.gvQuestions.DataBind(); this.gvQuestions.PageIndex = 0;
GvQuestions is the GridView control, ExamQuestionInfoList is the data source, and the gridview data source can be a able or DataSet.
3. Stay on a line to change color
private void ChangeColor(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#E6F5FA'"); e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c"); } }
Protected void gvQuestions_RowDataBound (object sender, GridViewRowEventArgs e)
{
ChangeColor (sender, e );
}
4. operate a row
Examples
Protected void gvSubjectiveOption_RowCommand (object sender, GridViewCommandEventArgs e) {int rowSelected = Convert. toInt32 (e. commandArgument); questionOptionInfo = QuestionOptionBLL. getModel (rowSelected); // view if (e. commandName = "ViewSOption") {this. tbOptionStem. text = questionOptionInfo. QO_Option; this. tbCorrectAnswer. text = questionOptionInfo. QO_SubjectAnswer; // subjective answer this. tbCorrectAnswerExplain. text = questionOptionInfo. QO_Explain; // option attachment string optionAccessoryStr = questionOptionInfo. QO_Accessory; string [] optionAccessoryArr = optionAccessoryStr. split (','); for (int I = 0; I <optionAccessoryArr. length; I ++) {OptionAccessoryList. add (optionAccessoryArr [I]);} BindOptionAccessoryList ();} if (e. commandName = "DeleteOption") {QuestionOptionBLL. delete (rowSelected); int EQ_ID = questionOptionInfo. EQ_ID; BindSubjectiveOption (EQ_ID); // rebind subjective question information }}
E. CommandName:
<Asp: TemplateField HeaderText = "View"> <ItemTemplate> <asp: linkButton ID = "LinkButtonViewSOption" runat = "server" CommandName = "ViewSOption" CommandArgument = '<% # Bind ("QO_ID") %>'> description </asp: linkButton> </ItemTemplate> <ItemStyle Width = "5%" HorizontalAlign = "Center"/> </asp: TemplateField> <asp: templateField HeaderText = "delete"> <ItemTemplate> <asp: ImageButton ID = "ImageButtonDelete2" runat = "server" Bor DerStyle = "None" CommandName = "DeleteOption" CommandArgument = '<% # Bind ("QO_ID") %> 'imageurl = "~ /Images/delete.gif "/> </ItemTemplate> <ItemStyle Width =" 5% "HorizontalAlign =" Center "/>
</Asp: TemplateField>
CommandName = "DeleteOption" CommandArgument = '<% # Bind ("QO_ID") %> indicates a field in the dataset.
5. Add a Checkbox and initialize the console:
<asp:TemplateField > <ItemTemplate > <asp:LinkButton ID ="LinkButton1" runat ="server" CommandName ="selectCorrectAnswer" CommandArgument ='<%# Bind("QO_ID") %>'> <asp:CheckBox ID ="cbCorrectAnswer" runat ="server" /> </asp:LinkButton>
</ItemTemplate>
Background logic:
/// <Summary> /// initialize the checkbox value /// </summary> /// <param name = "gv"> gridview Control </param> /// <param name = "dtSource"> data source </param> /// <param name = "cbName"> checkbox control name </param> /// <param name = "cbValue"> checkbox value </param> private void InitializeCheckBox (GridView gv, dataTable dtSource, string cbName, string cbValue) {int count = dtSource. rows. count; if (count> 0) {for (int I = 0; I <count; I ++) {Che CkBox cb = gv. Rows [I]. FindControl (cbName) as CheckBox; if (cb! = Null) {if (dtSource. rows [I] [cbValue]. toString () = "0") {cb. checked = false;} else {cb. checked = true ;}}}}}