坐上回传动态控制(唯一ID) [英] Get dynamic control (unique ID) on postback

查看:104
本文介绍了坐上回传动态控制(唯一ID)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在ASP.NET一些动态控件。我需要从不再存在控制检索回传的值。

I have some dynamic controls in ASP.NET. I need to retrieve a value on postback from a control that no longer exists.

以下code完美的作品:

The following code works perfectly:

value = Request.Form["ctl00$ContentPlaceHolder1$text_name"];

不过,我不知道是否有动态生成ctl00 $的ContentPlaceHolder $部分或获得的值一种优雅的方式textbox_value.UniqueID 当控件不再存在。

However, I'm wondering if there's an elegant way to dynamically generate the "ctl00$ContentPlaceHolder$" part or to get the value of textbox_value.UniqueID when the control no longer exists.

道歉,如果这个问题的拿出一百万次之前 - 只是寻找一个更好的解决方案,不涉及在服务器上更改任何设置

Apologies if this question's come up a million times before - just looking for a more elegant solution that doesn't involve changing any settings on the server.

编辑:

按照要求,code添加控制是这样的:

As requested, the code for adding controls is like this:

foreach (NameValuePair entry in database_table){

    Label label = new Label();
    label.ID = "label_" + entry.Name;
    label.Value = entry.Name;
    label.Attributes.Add("runat", "server");

    TextBox textBox = new TextBox();
    textBox.ID = "text_" + entry.Name;
    textBox.Text = entry.Value;
    textBox.Attributes.Add("runat", "server");
}

在点击一个按钮,它读取数据库表(条目的数量不是一成不变的)名称 - 值对,让他们进行修改并保存他们当保存按钮pressed。它的功能就像一个弹出窗口,使领域都走了上回发。 (我会preFER不改变code的这一部分)

On a button click, it reads name-value pairs from a database table (the number of entries is not static), allows them to be modified and saves them when a "save" button is pressed. It functions like a pop-up, so the fields are gone on postback. (I would prefer not to change this part of the code)

谢谢〜

推荐答案

我结束了写我自己的函数来解析的Request.Form 数据。它搜索名为 attname 控制和返回控制的值。

I ended up writing my own function to parse the Request.Form data. It searches for the control named attname and returns the value of the control.

private string findControlValue(string attname)
{
    string[] request = Request.Form.ToString().Split('&');
    string searchkey = "txt_" + attname;

    foreach (string seg in request)
    {
        if (seg.Contains(searchkey))
        {
            string ctrlName = seg.Split('=')[0];
            string ctrlValue = seg.Split('=')[1];
            string value = Server.UrlDecode(ctrlValue);
            return value;
        }
    }
    return null;
}

这篇关于坐上回传动态控制(唯一ID)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆