How a ComboBox in a ext.net binds a value in a database

Source: Internet
Author: User

This article shares the Nature Crunch blog, the address is: http://www.cnblogs.com/jianglan/archive/2012/03/29/2424177.html

Once again, the problem in the project today is that the values in the ComboBox are straightforward to bind. Simply add the item on the line. The code is as follows:

<ext:combobox id="Combox_secretslevel"Runat="Server"Fieldlabel="Classified"Width="250"Emptytext= " Please select the classification ...  <items> 
<ext:listitem text=" Span style= "color: #800000;" > public "Value= 1 "/>
<ext:listitem text=" confidentiality "Value=" 2 <ext:listitem text= " top secret "Value= 3 "/>
</items>
</ext:ComboBox>

But how do you get the bindings from the database?
Looking for the next online quality seems to be very little, to the official website to find some examples of the ComboBox. Although it is not written dead on the control, but found that he is simply by getting an array in the background, and then binding the data to operate, there is no real operation of the database. So I tried, combined with examples and the actual, to achieve the binding background database requirements, this side with you to share.
The parameters and values in this database

Get the data in the table as long as the simple SQL query statement, this side is not explained in detail.
In the page, the first is the code for the ASPX page:

<%@ Page language="C#"Autoeventwireup="True"Codebehind="LR_FileReg.aspx.cs"Inherits="EasyCreate.DFMS.WebUI.LR_FileReg"%>

<%@ Register assembly="Ext.net"Namespace="Ext.net"Tagprefix="Ext"%>

<! DOCTYPE HTML Public"-//W3C//DTD XHTML 1.0 transitional//en""Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

"Http://www.w3.org/1999/xhtml">
"Head1"Runat="Server">
<title> bind ComboBox Background Data </title>
<body>
<form id="Form1"Runat="Server">
<ext:resourcemanager id="ResourceManager1"Runat="Server"/>
<ext:store id="Store_secretscom"Runat="Server">
<Reader>
<ext:JsonReader>
<Fields>
<ext:recordfield name="Secretslevelid"Type="Int"/>
<ext:recordfield name="Secretslevelname"Type="String"/>
</Fields>
</ext:JsonReader>
</Reader>
</ext:Store>
<ext:combobox id="Combox_secretslevel"Runat="server dense "Width= 250 "Emptytext=" Please select the classification ... "
Storeid= store_ Secretscom "Valuefield=" secretslevelid "displayfield= "secretslevelname" >
</ext:ComboBox>
</form>
</body>

Background. cs File Code:

H_FILELR H_file =New H_FILELR ();//Invoking remote services and methods, perform basic database operations

Protectedvoid Page_Load (Object sender, EventArgs e)
{
if (! X.isajaxrequest)
{
//Executed at first load
DataSet ds_com= H_file. Secretcom ();//The value returned is data of type dataset
DataTable dt_com = ds_com.tables[0];//Get the data in the table, the ComboBox general parameters are two: value passed parameters and test display values
list<object> list =new list<object> (Dt_Com.Rows.Count);
foreach (DataRow dr_com in dt_com.rows) // traversal gets two values
{
list. ADD (new
{
Secretslevelid =int32.parse (dr_com[ "secretslevelid"]. ToString ()),
Secretslevelname = Dr_com[ " "]. ToString ()
});
}
Store_secretscom.datasource = list; // binding data
Store_ Secretscom.databind ();
}
} /span>

The following is the project, not very good, please forgive me:

In this way, simple data binding is implemented, in fact, it is quite simple. Everybody has any good method and the suggestion all remember to give me the message ha. The blog post for my original, reprint please indicate the source, respect the copyright Kazakhstan.

Continuation of the article:
Today, once again using the Comobox control to bind the values in the database, try to try the next new method, the effect is very good, and the previous method is much simpler. Below and we introduce, and can extrapolate.

 if (! IsPostBack)
{
DataSet ds_com = H_file.comoboxbind ();
DataTable Dt_secret = ds_com.tables[0];// Get the data in the table, the ComboBox general parameters are two: value passed parameters and test display values
foreach (DataRow dr_secret in dt_secret.ro WS) // traverse gets two values
{
Ext.Net.ListItem secretslist = new Ext.Net.ListItem (); // Create one Ext.Net.ListItem object at a time
Secretslist.value = Dr_secret[ "secretslevelid "]. ToString ();
Secretslist.text = Dr_secret[ "secretslevelname< Span style= "color: #800000;" >"]. ToString ();
ComBox_SecretsLevel.Items.Add (secretslist);
}
} /span>

The value of each row is assigned to the object's text and value properties, respectively. You can then add the Items.Add (secretslist) method to the list.
The advantage of this method is too obvious, without the last time the creation of the store, also do not have to bind Valuefield and Displayfield, there is more than a swoop!

In fact, when we write the value of the foreground, ignoring his essence, careful friends may be clever to find. In fact, every control in the foreground can be created in the background, the property can be re-assigned in the background changes.

<ext:combobox id="Combox_secretslevel"Runat="Server"Fieldlabel="Classified"Width="250"Emptytext= " Please select the classification ...  <items> 
<ext:listitem text=" Span style= "color: #800000;" > public "Value= 1 "/>
<ext:listitem text=" confidentiality "Value=" 2 <ext:listitem text= " top secret "Value= 3 "/>
</items>
</ext:ComboBox>

We can subtly discover: ComBox_SecretsLevel.Items.Add (secretslist), which is equivalent to adding each item to ListItem.

This time makes me more firm belief, as long as insist, there will be a harvest. Even a little progress every day is a new improvement.

How a ComboBox in a ext.net binds a value in a database

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.