15:25:52
新加三层查找id
protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e) { if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) { RadioButtonList RadList = (RadioButtonList)e.Item.FindControl("rblOption"); // 方式一,使用隐藏控件来取ID //HiddenField hidQID = (HiddenField)e.Item.FindControl("hidQID"); //string QID = hidQID.Value.ToString(); //方式二 使用Model型层来获取id. MODEL.exam ss = (MODEL.exam)e.Item.DataItem; string QID = ss.Id.ToString(); GetExam bll = new GetExam(); List<Option> opts = bll.getListAllExamOption(QID); foreach (Option opt in opts) { RadList.Items.Add(new ListItem(opt.Opt.ToString(), opt.Score.ToString())); } } }
在.net数据控件中绑定了一些常用控件,我们的要对这些控件进行操作的话,就要编写代码对控件ID进行查找。一般的用ID来查找是不行。现在总结下如何使用。
代码如下:
protected void rptPaper_ItemDataBound(object sender, RepeaterItemEventArgs e) { if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) { ListBox lb = (ListBox)e.Item.FindControl("ListBox1"); //listbox //Panel PanelShowText = (Panel)e.Item.FindControl("PanelShowText"); //Panel PanelImgShow = (Panel)e.Item.FindControl("PanelImgShow"); //Image imgShow = (Image)e.Item.FindControl("imgShow"); //image //string QID = ((DataRowView)e.Item.DataItem).Row["ID"].ToString(); //datarowview 是查找repeater的ID //string strsql = "SELECT * FROM radiotable where ID='" + QID + "'"; //DataTable dt = DtDB.GetDataTable(strsql); //if (dt.Rows.Count > 0) //{ // 判断是否有图 // string imgpath = dt.Rows[0]["imgPath"].ToString(); // if (imgpath == null || imgpath == "") // { // PanelImgShow.Visible = false; // PanelShowText.Visible = true; // } // else // { // PanelShowText.Visible = false; // PanelImgShow.Visible = true; // imgShow.ImageUrl = "~/upFile/" + imgpath; // } // tbContent.Text = dt.Rows[0]["QTitle"].ToString(); // string ss = dt.Rows[0]["QContent"].ToString(); // string Qfeng = dt.Rows[0]["QFeng"].ToString(); // //string // string[] sArr = ss.Split('|'); // string[] sArrQFeng = Qfeng.Split('|'); // RadList.Items.Clear(); // for (int i = 0; i < (sArr.Length - 1); i++) // { // RadList.Items.Add(new ListItem(sArr[i].ToString().Trim(), sArrQFeng[i].ToString().Trim())); // //lblFeng.Text += sArrCx[i].ToString().Trim(); // //RadList.Items = "styled"; // } //} //dt.Dispose(); //RadList.DataSource= } }
这是在repeater中进行查找。在repeater控件外进行查找时。
代码如下:
protected void btnRest_Click(object sender, EventArgs e) { for (int i = 0; i < this.rptPaper.Items.Count; i++) { ListBox lb2 = (ListBox)this.rptPaper.Items[i].FindControl("ListBox2"); lb2.Items.Clear(); } BuildPaper(); }
当然在repeater中的按钮事件可以用如下方法:
protected void RptContact_ItemCommand(object source, RepeaterCommandEventArgs e) { if (e.CommandName == "Delete") { long ContID = Convert.ToInt64(e.CommandArgument.ToString()) ; DeleteContact(ContID); DataBindContact(); } }
这是一些常用的按钮事件。网上有很多详细介绍。
这里就不再多提了。我主要介绍下在repeater中的一些不是按钮事件的指定。如:ListBox中的SelectedIndexChanged事件。本事件不用在repeater中的ItemCommand中写了。跟平时使用该事件的编写是一样的。
代码如下:
protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e) { for (int i = 0; i < this.rptPaper.Items.Count; i++) { ListBox lb = (ListBox)this.rptPaper.Items[i].FindControl("ListBox1"); ListBox lb2 = (ListBox)this.rptPaper.Items[i].FindControl("ListBox2"); TextBox tbHidAnswer = (TextBox)rptPaper.Items[i].FindControl("tbHidAnswer"); string slb = lb.SelectedValue; tbHidAnswer.Text += slb + ","; DtDB.AlertForPage(Page, tbHidAnswer.Text); if (lb.SelectedIndex != -1) //有选中的 { foreach (ListItem item in lb.Items) { if (item.Selected) { lb2.Items.Add(item); } } } lb.Items.RemoveAt(lb.SelectedIndex); } }
附加:
linkbutton
protected void ltbnPre_click(object sender, EventArgs e) { string Pid = ((LinkButton)sender).CommandArgument.ToString(); }
注:此例是在VS2008中编译通过。