为了账号安全,请及时绑定邮箱和手机立即绑定

获取DataList控件的主键和索引

标签:
JavaScript


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);            }        }    }


 

点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号

举报

0/150
提交
取消