獲取DataList控件的主鍵和索引實用圖解
更新時間:2013年01月20日 14:10:21 作者:
一是在DataList控件添加一個DataKeyField,以便獲取到它的主鍵值,另外還添加了兩個銨鈕及一個Label標答,用來顯示選擇結果,真正將來你也許用不上標簽,因為獲取到結果之后,就可以進行你想的要事情了
“哈哈,看了這篇http://www.dbjr.com.cn/article/33388.htm,只有效果,但沒有結果,我怎樣獲取選擇行的相關記錄?”
Insus.NET首先多謝網(wǎng)友對上面那篇博文的關注。解決你的問題,可以嘗試獲取選擇行的索引或是主鍵即可,能獲取到主鍵,其它字段的值,也可以獲取到了。
下圖中,高亮選擇區(qū),即是針對問題解決而在原在代碼添加的部分。一是在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("你執(zhí)行銨鈕'{0}',選擇行的索引是:{1}; 主鍵值是:{2}", button.Text, dli.ItemIndex.ToString(), dlconstellation.DataKeys[dli.ItemIndex].ToString());
break;
}
else
{
LabelSelectedResult.Text = string.Format("你執(zhí)行銨鈕'{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("你執(zhí)行銨鈕'{0}',選擇行的索引是:{1}; 主鍵值是:{2}", button.Text, i.ToString(), dlconstellation.DataKeys[i].ToString());
break;
}
else
{
LabelSelectedResult.Text = string.Format("你執(zhí)行銨鈕'{0}',沒有選擇任何一行。", button.Text);
}
}
}
Insus.NET首先多謝網(wǎng)友對上面那篇博文的關注。解決你的問題,可以嘗試獲取選擇行的索引或是主鍵即可,能獲取到主鍵,其它字段的值,也可以獲取到了。
下圖中,高亮選擇區(qū),即是針對問題解決而在原在代碼添加的部分。一是在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("你執(zhí)行銨鈕'{0}',選擇行的索引是:{1}; 主鍵值是:{2}", button.Text, dli.ItemIndex.ToString(), dlconstellation.DataKeys[dli.ItemIndex].ToString());
break;
}
else
{
LabelSelectedResult.Text = string.Format("你執(zhí)行銨鈕'{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("你執(zhí)行銨鈕'{0}',選擇行的索引是:{1}; 主鍵值是:{2}", button.Text, i.ToString(), dlconstellation.DataKeys[i].ToString());
break;
}
else
{
LabelSelectedResult.Text = string.Format("你執(zhí)行銨鈕'{0}',沒有選擇任何一行。", button.Text);
}
}
}
相關文章
log4net創(chuàng)建系統(tǒng)日志的詳細步驟
log4net是.Net下一個非常優(yōu)秀的開源日志記錄組件。log4net記錄日志的功能非常強大。它可以將日志分不同的等級,以不同的格式,輸出到不同的媒介。本文主要是簡單的介紹如何在Visual Studio2010(Asp.Net Mvc3.0)中使用log4net快速創(chuàng)建系統(tǒng)日志,如何擴展以輸出自定義字段2013-11-11.Net平臺開發(fā)實踐的一些點滴總結(技術規(guī)范與實踐精華)
以下是本人對.Net平臺開發(fā)實踐的一些點滴總結。這里的技術規(guī)范主要是開發(fā)過程的代碼規(guī)范、數(shù)據(jù)庫設計規(guī)范、Com和.Net互操作規(guī)范;實踐精華是對技術實踐過程中的部分總結。2010-04-04asp.net通過js實現(xiàn)Cookie創(chuàng)建以及清除Cookie數(shù)組的代碼
asp.net Cookie創(chuàng)建以及清除Cookie數(shù)組2010-03-03net core下鏈路追蹤skywalking安裝和簡單使用教程
本文將從0開始搭建兩個webapi項目,使用Skywalking來追蹤他們之間的調用關系及響應時間,開發(fā)環(huán)境為VisualStudio2019,對net core 鏈路追蹤skywalking安裝和使用教程感興趣的朋友一起看看吧2021-10-10詳解Asp.Net MVC——控制器與動作(Controller And Action)
這篇文章主要介紹了詳解Asp.Net MVC——控制器與動作(Controller And Action) ,具有一定的參考價值,感興趣的小伙伴們可以參考一下。2017-03-03asp.net BOF或EOF有一個是真,或者當前記錄已被刪除
在調試“學生管理系統(tǒng)”時,想修改學籍信息,如果此時數(shù)據(jù)庫中的數(shù)據(jù)已被完全刪除則會出現(xiàn)“BoF 或Eof有一個是真,或當前記錄已被刪除的錯誤2009-07-07.NET讀寫Excel工具Spire.Xls使用 Excel文件的控制(2)
這篇文章主要為大家詳細介紹了.NET讀寫Excel工具Spire.Xls使用,Excel文件的控制,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-11-11