Implement two dropdownlist-level changes under the detailsview control in Asp.net

Source: Internet
Author: User

Http://www.cnblogs.com/dreampeng007/archive/2007/12/03/980965.html

 

During the project, I have been using the detailsview control to display the detailed data of the database table. Today, when I use detailsview to display data, if I click Edit, you need to display the two drop-down lists of villages and villages. When you click villages, the villages need to correspond to the two drop-down lists.
 
Solution:
In the detailsview control, add two template columns for the township and village respectively, and add two controls under the edititemtemplate of the two template columns:
Hiddenfield and dropdownlist. hiddenfield is used to record the number of the township (or village) in the current database, and dropdownlist is used to display
This data must be dynamically bound according to the information you have edited when you click Edit. Therefore, this code should be written to the detailsview Control
In the modechanging event, the connection between the township and the village is very simple. Like the implementation principle on the page, set the property autopostback of the dropdownlist control of the township.
If it is true, you can add the onselectedindexchanged event.
 
Some code:
Detailsview:
<Asp: templatefield headertext = "">

<Edititemtemplate> <asp: hiddenfield id = "hdxiang" runat = "server"
Value = '<% # BIND ("") %>'/> <asp: dropdownlist id = "ddxiangz"
Runat = "server" width = "90px" autopostback = "true"
Onselectedindexchanged = "ddlxiang_selectedindexchanged"/> </edititemtemplate>
<Itemtemplate>
<Asp: Label
Id = "label1" runat = "server"
TEXT = '<% # getxiangname (eval (""). tostring ())
%> '> </ASP: Label>
</Itemtemplate>
</ASP: templatefield>
<Asp: templatefield headertext = "village name">

<Edititemtemplate> <asp: hiddenfield id = "hdcun" runat = "server"
Value = '<% # BIND ("village name") %>'/> <asp: dropdownlist id = "ddlcun"
Runat = "server" width = "90px"/> </edititemtemplate>
<Itemtemplate>
<Asp: Label
Id = "label2" runat = "server" text = '<% #
Getcunname (eval ("village name"). tostring () %> '> </ASP: Label>
</Itemtemplate>
</ASP: templatefield>
 
Modechanging event:
If (E. newmode = detailsviewmode. Edit)
{
// Bind the home address, search for it from the detailsview control, and find my dropdownlist home in the third line
Dropdownlist ddxiang = (dropdownlist) This. detailsview1.rows [2]. cells [1]. controls [0]. findcontrol ("ddxiangz ");
Ddxiang. datasource = your data source;
Ddxiang. datatextfield = "name ";
Ddxiang. datavaluefield = "no ";
Ddxiang. databind ();
// Select the dropdownlist of the township
Ddxiang. selectedvalue = (hiddenfield) This. detailsview1.rows [2]. cells [1]. findcontrol ("hdxiang"). value;

// Bind a village, search for it from the detailsview control, and find my dropdownlist village in the fourth row
Dropdownlist ddcun = (dropdownlist) This. detailsview1.rows [3]. cells [1]. controls [0]. findcontrol ("ddlcun ");
Ddcun. datasource = your data source;
Ddcun. datatextfield = "village name ";
Ddcun. datavaluefield = "no ";
Ddcun. databind ();
// Select the dropdownlist of the queue
Ddcun. selectedvalue = (hiddenfield) This. detailsview1.rows [3]. cells [1]. findcontrol ("hdcun"). value;
}
 
/// <Summary>
/// When editing, the village changes with the village
/// </Summary>
/// <Param name = "sender"> </param>
/// <Param name = "E"> </param>
Public void ddlxiang_selectedindexchanged (Object sender, eventargs E)
{
// Bind a village
Dropdownlist ddcun = (dropdownlist) This. detailsview1.rows [4]. cells [1]. controls [0]. findcontrol ("ddlcun ");
Dropdownlist ddxiang = (dropdownlist) This. detailsview1.rows [3]. cells [1]. controls [0]. findcontrol ("ddxiangz ");
Ddcun. datasource = mycom your data source;
Ddcun. datatextfield = "village name ";
Ddcun. datavaluefield = "no ";
Ddcun. databind ();
}
 
:




Web development technology knowledge base: http://www.cn-web.com/cnweb/0/385/article/

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.