我正在开发一个项目,我正在使用转发器控件,在其项目数据绑定事件中,我正在添加一些数据和两个按钮 - 编辑和删除。但是我的按钮的单击事件不起作用,当我点击任何在这两个按钮中,我的转发器数据消失了。
以下是我的代码
protected void fillrptmapping()
{
ds = objcce1.selectSubject(schoolcode);
rptmapping.DataSource = ds;
rptmapping.DataBind();
}
protected void rep_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
Label lblsubcode = (Label)e.Item.FindControl("lblsubcode");
Table tblmapping = (Table)e.Item.FindControl("tblmapping");
int subcode =Convert.ToInt32(lblsubcode.Text.ToString());
DataSet ds1 = new DataSet();
ds1 = objcce1.selectSubjectskillmapping(schoolcode);
DataSet ds2 = new DataSet();
ds2 = objcce1.selectAssessfullmapping(schoolcode);
DataRow[] dr1;
DataRow[] dr2;
TableRow tr = new TableRow();
tr.Width = 300;
TableCell tc = new TableCell();
tr.Width = 300;
tc.Height = 100;
//selecting skillmappingcodes
dr1 = ds1.Tables[0].Select("subjectgroupcode='" + subcode + "'");
for (int j = 0; j < dr1.Length; j++)
{
Label lblskill = new Label();
lblskill.CssClass = "lblskilltxt";
lblskill.Text = dr1[j]["skillname"].ToString() + "<br/>";
tc.Controls.Add(lblskill);
dr2 = ds2.Tables[0].Select("skillmappingcode='" + dr1[j]["skillmappingcode"] + "'");
for (int k = 0; k < dr2.Length; k++)
{
ImageButton imgedit = new ImageButton();
imgedit.ImageUrl = "../Images/edit.jpg";
imgedit.Width = 25;
imgedit.Height = 25;
imgedit.ID = dr2[k]["assessmentareamappingcode"].ToString();
imgedit.Click += new ImageClickEventHandler(imgeditassessmapping_OnClick);
//Button btnedit = new Button();
//btnedit.ID = dr2[k]["assessmentareamappingcode"].ToString();
//btnedit.Click += new System.EventHandler(this.imgeditassessmapping_OnClick);
ImageButton imgdelete = new ImageButton();
imgdelete.Width = 25;
imgdelete.Height = 25;
imgdelete.ImageUrl = "../Images/delete.jpg";
imgdelete.Click += new ImageClickEventHandler(this.imgdeleteassessmapping_OnClick);
Label lblarea = new Label();
lblarea.Text = dr2[k]["assessmentareaname"].ToString() + "<br/><br/>";
tc.Controls.Add(btnedit);
tc.Controls.Add(imgdelete);
tc.Controls.Add(lblarea);
}
}
//selecting subjectgroup in area table
dr2 = ds2.Tables[0].Select("subgroupcode='" + subcode + "'");
for (int l = 0; l < dr2.Length; l++)
{
ImageButton imgedit = new ImageButton();
imgedit.Width = 25;
imgedit.Height = 25;
imgedit.ImageUrl = "../Images/edit.jpg";
imgedit.Click += new ImageClickEventHandler(this.imgeditassessmapping2_OnClick);
ImageButton imgdelete = new ImageButton();
imgdelete.Width = 25;
imgdelete.Height = 25;
imgdelete.ImageUrl = "../Images/delete.jpg";
imgdelete.Click += new ImageClickEventHandler(this.imgdeleteassessmapping2_OnClick);
Label lblarea = new Label();
lblarea.Text = dr2[l]["assessmentareaname"].ToString() + "<br/>";
tc.Controls.Add(imgedit);
tc.Controls.Add(imgdelete);
tc.Controls.Add(lblarea);
}
tr.Cells.Add(tc);
tblmapping.Rows.Add(tr);
}
}
protected void imgeditassessmapping_OnClick(object sender,EventArgs e)
{
Button img = sender as Button;
string assessmappingcode = img.ID;
ds =objcce1.selectAssessSkill(schoolcode, assessmappingcode);
foreach (ListItem item in drpsubjectoption.Items)
{
if (item.Value == ds.Tables[0].Rows[0]["subjectgroupcode"].ToString())
{
item.Selected = true;
}
}
rblmapping.SelectedIndex = 0;
spanskill.Visible = true;
drpmappingskill.Visible = true;
foreach (ListItem item in drpmappingskill.Items)
{
if (item.Value == ds.Tables[0].Rows[0]["skillcode"].ToString())
{
item.Selected = true;
}
}
pnlassessmapping.Visible = true;
foreach (ListItem item in drpassessmapping.Items)
{
if (item.Value == ds.Tables[0].Rows[0]["assessmentareacode"].ToString())
{
item.Selected = true;
}
}
foreach (ListItem item in drpmarkingtype.Items)
{
if (item.Text == ds.Tables[0].Rows[0]["type"].ToString())
{
item.Selected = true;
}
}
foreach (ListItem item in rbldesc.Items)
{
if (item.Text == ds.Tables[0].Rows[0]["desindreq"].ToString())
{
item.Selected = true;
}
}
btnsavemapping.Text = "Update";
}
protected void imgdeleteassessmapping_OnClick(object sender,ImageClickEventArgs e)
{
}
ASPX页面代码
<asp:Repeater ID="rptmapping" runat="server" OnItemDataBound="rep_ItemDataBound" >
<HeaderTemplate></HeaderTemplate>
<ItemTemplate>
<table>
<tr>
<td>
<asp:Label ID="lblsubcode" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "subgroupcode")%>'></asp:Label>
<asp:Label ID="lblsubject" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "subname")%>'></asp:Label>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="imgeditassessmapping_OnClick"/>
</td>
</tr>
<tr>
<td>
<asp:Table ID="tblmapping" runat="server" Width="300" BorderColor="Black" GridLines="both"></asp:Table>
</td>
</tr>
</table>
</ItemTemplate>
</asp:Repeater>
点击按钮之前的屏幕截图
单击按钮后的
屏幕截图
答案 0 :(得分:1)
您需要了解动态控件的生成方式。每次页面加载时(即首次加载时和后续加载/回发时),都需要使用相同的ID生成它们。
如果该按钮在页面生命周期阶段不存在(尚未生成),它触发事件处理程序(例如按钮单击事件),则事件处理程序将不会触发。
通常,症状表明您的ItemDataBound事件处理程序未在回发时触发;如果请求不是回发,您可能只是数据绑定。让每个页面请求触发事件都会有所帮助,但总体而言,如果您可以始终在页面上拥有这些按钮,而不是动态生成它们会更好。那会给你一个更可靠的事件处理程序。