使用 Repeater 实现可更新的表单

发布于 2024-08-23 01:17:00 字数 1487 浏览 1 评论 0原文

我有一个参数名称列表,我希望用户输入一些值,所以我这样做:

<div>
    <asp:Repeater runat="server" ID="rptTemplateParams" EnableViewState="true">
    <HeaderTemplate>
        <ul>
    </HeaderTemplate>
    <ItemTemplate>
        <li>
            <asp:Label runat="server"><%#Container.DataItem%></asp:Label>
            <asp:TextBox runat="server" ID="textParamValue"></asp:TextBox>
        </li>
    </ItemTemplate>
    <FooterTemplate>
        </ul>
    </FooterTemplate>
    </asp:Repeater>
</div>
<asp:Button runat="server" ID="Send" Text="Send Email" OnClick="Send_Click" />

在服务器端:

void Page_Load(...)
{
    rptTemplateParams.DataSource =Params;  // Params is List<string>
    rptTemplateParams.DataBind();
}



public void Send_Click(object sender, EventArgs e)
{
    ParamDict = new Dictionary<string, string>();
    foreach (RepeaterItem item in rptTemplateParams.Items)
    {
    if (item.ItemType == ListItemType.Item)
    {
        TextBox textParamValue = (TextBox)item.FindControl("textParamValue");
        if (textParamValue.Text.Trim() != String.Empty)
        {
            // IT NEVER GETS HERE - textParamValue.Text IS ALWAYS EMPTY!!!

            ParamDict.Add(item.DataItem.ToString(), textParamValue.Text);
        }
    }
    }
}

当我输入注释时,我无法检索文本框值 - 它们始终为空。我是否在错误的地方检索它们?

谢谢! 安德烈

I have a list of parameter names for which I want a user to type in some values, so I do this:

<div>
    <asp:Repeater runat="server" ID="rptTemplateParams" EnableViewState="true">
    <HeaderTemplate>
        <ul>
    </HeaderTemplate>
    <ItemTemplate>
        <li>
            <asp:Label runat="server"><%#Container.DataItem%></asp:Label>
            <asp:TextBox runat="server" ID="textParamValue"></asp:TextBox>
        </li>
    </ItemTemplate>
    <FooterTemplate>
        </ul>
    </FooterTemplate>
    </asp:Repeater>
</div>
<asp:Button runat="server" ID="Send" Text="Send Email" OnClick="Send_Click" />

and on server side:

void Page_Load(...)
{
    rptTemplateParams.DataSource =Params;  // Params is List<string>
    rptTemplateParams.DataBind();
}



public void Send_Click(object sender, EventArgs e)
{
    ParamDict = new Dictionary<string, string>();
    foreach (RepeaterItem item in rptTemplateParams.Items)
    {
    if (item.ItemType == ListItemType.Item)
    {
        TextBox textParamValue = (TextBox)item.FindControl("textParamValue");
        if (textParamValue.Text.Trim() != String.Empty)
        {
            // IT NEVER GETS HERE - textParamValue.Text IS ALWAYS EMPTY!!!

            ParamDict.Add(item.DataItem.ToString(), textParamValue.Text);
        }
    }
    }
}

As I put in the comment, i can't retrieve text box values - they are always empty. Am I retrieving those in the wrong place?

Thanks!
Andrey

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

甜宝宝 2024-08-30 01:17:00

尝试像这样修改您的 page_load

if(!Page.IsPostBack)
{
    rptTemplateParams.DataSource =Params;  // Params is List<string> 
    rptTemplateParams.DataBind(); 

}

数据绑定会清除现有控件并用新的空白控件替换它们。您只想在必要时进行数据绑定。

Try modifying your page_load like this

if(!Page.IsPostBack)
{
    rptTemplateParams.DataSource =Params;  // Params is List<string> 
    rptTemplateParams.DataBind(); 

}

The databinding wipes out existing controls and replaces them with new blank controls. You only want to databind when necessary.

染墨丶若流云 2024-08-30 01:17:00

试试这个:

void Page_Load(...)
{
    if (!IsPostBack)
    {
        rptTemplateParams.DataSource =Params;  // Params is List<string>
        rptTemplateParams.DataBind();
    }
}

try this:

void Page_Load(...)
{
    if (!IsPostBack)
    {
        rptTemplateParams.DataSource =Params;  // Params is List<string>
        rptTemplateParams.DataBind();
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文