Obtains the primary key and index diagrams of the DataList control.

Source: Internet
Author: User

"Haha, read this article ?"

Insus. NET first of all, I would like to thank the netizens for their attention to the previous blog. To solve your problem, you can try to get the index or primary key of the selected row. You can get the primary key and the value of other fields.

In the highlighted selection area, that is, to solve the problem and add it in the original code. First, add a DataKeyField to the DataList control to obtain its primary key value. In addition, two buttons and a Label are added to display the selection result, in the future, you may not be able to use tags, because after obtaining the results, you can do what you want.

The two buttons are simple, and Insus. NET is believed to be understandable. If you encounter an unknown event, you can discuss:
Copy codeThe Code is as follows:
View Code
Protected void button#click (object sender, EventArgs e)
{
Button button = (Button) sender;
If (FindControl ("DataListConstellation") = null) return;
DataList dlconstellation = (DataList) FindControl ("DataListConstellation ");
Foreach (DataListItem dli in dlconstellation. Items)
{
If (dli. FindControl ("RadioButtonSelect") = null) return;
RadioButton rb = (RadioButton) dli. FindControl ("RadioButtonSelect ");
If (rb. Checked)
{
LabelSelectedResult. text = string. format ("You execute the button '{0}' and select the row index: {1}; primary key value: {2}", button. text, dli. itemIndex. toString (), dlconstellation. dataKeys [dli. itemIndex]. toString ());
Break;
}
Else
{
LabelSelectedResult. Text = string. Format ("You executed the button '{0}' and did not select any row. ", Button. Text );
}
}
}
Protected void Button2_Click (object sender, EventArgs e)
{
Button button = (Button) sender;
DataList dlconstellation = (DataList) FindControl ("DataListConstellation ");
For (int I = 0; I <dlconstellation. Items. Count; I ++)
{
RadioButton rb = (RadioButton) dlconstellation. Items [I]. FindControl ("RadioButtonSelect ");
If (rb. Checked)
{
LabelSelectedResult. text = string. format ("You execute the button '{0}' and select the row index: {1}; primary key value: {2}", button. text, I. toString (), dlconstellation. dataKeys [I]. toString ());
Break;
}
Else
{
LabelSelectedResult. Text = string. Format ("You executed the button '{0}' and did not select any row. ", Button. Text );
}
}
}

Related 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.