获取DataList控件的主键和索引实用图解
网络编程 2021-07-04 22:40www.168986.cn编程入门
一是在DataList控件添加一个DataKeyField,以便获取到它的主键值,还添加了两个铵钮及一个Label标答,用来显示选择结果,真正将来你也许用不上标签,因为获取到结果之后,就可以进行你想的要事情了
“哈哈,看了这篇,只有效果,但没有结果,我怎样获取选择行的相关记录?”
Insus.NET多谢网友对上面那篇博文的关注。解决你的问题,可以尝试获取选择行的索引或是主键即可,能获取到主键,其它字段的值,也可以获取到了。
下图中,高亮选择区,即是针对问题解决而在原在代码添加的部分。一是在DataList控件添加一个DataKeyField,以便获取到它的主键值,还添加了两个铵钮及一个Label标答,用来显示选择结果,真正将来你也许用不上标签,因为获取到结果之后,就可以进行你想的要事情了。
两个铵钮事件,都是很简单,Insus.NET相信你能看得懂,如果遇上不明,可以讨论
View Code
protected void Button1_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("你执行铵钮'{0}',选择行的索引是{1}; 主键值是{2}", button.Text, dli.ItemIndex.ToString(), dlconstellation.DataKeys[dli.ItemIndex].ToString());
break;
}
else
{
LabelSelectedResult.Text = string.Format("你执行铵钮'{0}',没有选择任何一行。", 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("你执行铵钮'{0}',选择行的索引是{1}; 主键值是{2}", button.Text, i.ToString(), dlconstellation.DataKeys[i].ToString());
break;
}
else
{
LabelSelectedResult.Text = string.Format("你执行铵钮'{0}',没有选择任何一行。", button.Text);
}
}
}
Insus.NET多谢网友对上面那篇博文的关注。解决你的问题,可以尝试获取选择行的索引或是主键即可,能获取到主键,其它字段的值,也可以获取到了。
下图中,高亮选择区,即是针对问题解决而在原在代码添加的部分。一是在DataList控件添加一个DataKeyField,以便获取到它的主键值,还添加了两个铵钮及一个Label标答,用来显示选择结果,真正将来你也许用不上标签,因为获取到结果之后,就可以进行你想的要事情了。
两个铵钮事件,都是很简单,Insus.NET相信你能看得懂,如果遇上不明,可以讨论
代码如下:
View Code
protected void Button1_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("你执行铵钮'{0}',选择行的索引是{1}; 主键值是{2}", button.Text, dli.ItemIndex.ToString(), dlconstellation.DataKeys[dli.ItemIndex].ToString());
break;
}
else
{
LabelSelectedResult.Text = string.Format("你执行铵钮'{0}',没有选择任何一行。", 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("你执行铵钮'{0}',选择行的索引是{1}; 主键值是{2}", button.Text, i.ToString(), dlconstellation.DataKeys[i].ToString());
break;
}
else
{
LabelSelectedResult.Text = string.Format("你执行铵钮'{0}',没有选择任何一行。", button.Text);
}
}
}
编程语言
- 宿迁百度关键词排名指南:实现精准营销的关键
- 四川SEO优化怎么做网络推广
- 立昂技术备案老域名收购:如何为您的业务赋能
- 安徽百度关键词seo贵不贵,一般需要多少钱
- 吉林百度快照排名怎么做电话营销
- 多伦新手做SEO怎么做
- 甘肃优化关键词排名推广怎么做论坛营销
- 沙雅SEO网站推广:提升您的在线可见性
- 四川SEO优化如何提升销售额和销售量
- 聂荣网站排名优化:提升网站可见性的全方位指
- 涞水SEO:提升地方企业在线可见性的策略
- 辽宁百度seo排名怎样做网站排名
- 临湘哪有关键词排名优化:提升网站可见度的关
- 黑龙江百度网站优化有没有优惠
- 凉城优化关键词排名推广:提升您的网络可见性
- 萝北整站优化:提升您网站流量和排名的全面指