使用InnerHtml渲染<input type =“submit”/>

时间:2013-06-18 09:24:41

标签: c# asp.net webforms

我目前正在开发一个Web表单项目,在后面的代码中我使用InnerHtml循环显示项目列表以显示它。

像这样的东西。

email = v.DomainObjectFields.Where(t => t.FieldName == "Email Address").First().FieldValue;
            name = v.DomainObjectFields.Where(t => t.FieldName == "First Name").First().FieldValue;
            familyName = v.DomainObjectFields.Where(t => t.FieldName == "Last Name").First().FieldValue;
            dateJoined = v.DomainObjectFields.Where(t => t.FieldName == "Date Joined").First().FieldValue;

            Users.InnerHtml += "<tr><td>";
            Users.InnerHtml += "<a href='UserDetail.aspx?username=" + email + "'>"+email+"</a>";
            Users.InnerHtml += "</td>";
            Users.InnerHtml += "<td>";
            Users.InnerHtml += name;
            Users.InnerHtml += "</td>";
            Users.InnerHtml += "<td>";
            Users.InnerHtml += familyName;
            Users.InnerHtml += "</td>";
            Users.InnerHtml += "<td>";
            Users.InnerHtml += dateJoined;
            Users.InnerHtml += "</td>";
            Users.InnerHtml += "<td>";
            **Users.InnerHtml += "<input type='button' id='butResetPassword' value='Resend Password'/>";**
            Users.InnerHtml += "</td>";
            Users.InnerHtml += "</tr>";

<div id="Users" runat="server">

</div>

但是按钮没有渲染,我需要在网格显示中添加一个按钮。有没有办法按照我现在的方式添加按钮和处理程序?

2 个答案:

答案 0 :(得分:1)

为什么不使用转发器或DataList?

protected void Page_Load(object sender, EventArgs e)
{
     rptUsers.DataSource = users;
     rptUsers.DataBind();
}

protected void rptUsers_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
      if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
      {
         User user = (User)e.Item.DataItem;
         Label email = (Label)e.Item.FindControl("email");
         Label name = (Label)e.Item.FindControl("name");
         Label familyname = (Label)e.Item.FindControl("familyname");
         Label datejoined = (Label)e.Item.FindControl("datejoined");

         email.Text = user.EmalAddress;
         name.Text = user.Name;
         familyname.Text = user.FamilyName;
         datejoined.Text = user.DateJoined.ToString();
      }
 }

 protected void btnSubmit_OnClick(object sender, EventArgs e)
 {
     //submit
 }

基本标记:

<asp:Repeater ID="rptUsers" runat="server" OnItemDataBound="rptUsers_OnItemDataBound">
   <ItemTemplate>
      <asp:Label id="email" runat="server" />
      <asp:Label id="name" runat="server" />
      <asp:Label id="familyname" runat="server" />
      <asp:Label id="datejoined" runat="server" />
   </ItemTemplate>
</asp:Repeater>

<asp:Button ID="btnSubmit" runat="server" OnClick="btnSubmit_OnClick" Text="Submit" />

你当然可以使用DataList,它的设置方式大致相同,只不过它可以为你呈现一个看似你想要的表格

答案 1 :(得分:0)

我认为以下href无法正确创建存在问题。

 Users.InnerHtml += "<a href='UserDetail.aspx?username=" + email + "'>"+email+"</a>";

尝试通过将此行替换为

来解决此问题
Users.InnerHtml += "<a href='UserDetail.aspx?username='" + email + "''>"+email+"</a>";