c# - 不加载发回的表单值?

标签 c# asp.net

我使用 Firebug 进行验证,并且我的回调已成功触发。在“Post”部分,它显示我的 TextBox(更确切地说,为我的文本框呈现的 HTML)已成功提交其表单值(无论在文本框中输入的内容)。然而,在服务器端,该值不会恢复到新创建的 TextBox 中——即使我重新创建的所有内容都具有与最初相同的 ID。此外,我检查了 Page.Request.Form 集合(请参阅:getControlAsHtml()),并且我尝试的所有操作都没有返回我的值。这是代码,感谢任何帮助。

摘要

我的目标是执行回调(当前正在工作)并从回发的表单值中恢复我的 TextBox 的值(不工作)。为了简洁起见,省略了一些(工作)代码。

public class CreateForm : WebPart, ICallbackEventHandler
{
    public string callbackData = "";
    public DropDownList list = new DropDownList();
    public Panel p = new Panel();

    public virtual string GetCallbackResult()
    {
        return callbackData;
    }

    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);

            //The DropDownList is created here in my real code, but was omitted
            //because it is working as expected.
            //At the moment, all the DDL does is cause a callback to occur.

        p.ID = "theForm";

        if (!Page.IsPostBack) 
        {
            MyForm MyForm = new MyForm();
            MyForm.ID = "currentForm";
            p.Controls.Add(MyForm);
        }

        Controls.Add(p);  
    }

    /*
     * The eventArgument field is parsed to get the desired command.
     * Valid commands:
     * 1) send - Indicates the user has clicked the 'send' button. 
     * 2) display - Indicates the user wants to change the form
     *              currently displayed.
     *    A drop down box is used to let the user choose the form. 
     */
    public virtual void RaiseCallbackEvent(string eventArgument)
    {
                if (eventArgument.StartsWith("display"))
            {
                //always index 0 for testing. 
                callbackData = CreateFormByType(0);
                }
    }

    public string CreateFormByType(int index)
    {
        MyForm test = null;

        switch (index)
        {
            case 0:
                test = new MyForm();
                break;
        }

        if (test != null)
        {
            test.ID = "currentForm";
            p.Controls.Add(test);
        }

        return test != null ? test.getControlAsHtml() : null;
    }

}

public class MyForm : Control
{
    public Label label = new Label();
    public TextBox textboxForLabel = new TextBox();

    protected override void OnInit(EventArgs e)
    {
            base.OnInit(e);

        textboxForLabel.ID = "textboxForLabel";
        label.AssociatedControlID = "textboxForLabel";
        label.Text = "textboxForLabel: ";

        this.Controls.Add(label);
        this.Controls.Add(textboxForLabel);
    }

    public virtual string getControlAsHtml()
    {
            //How come all 3 of these don't return the expected value?
        string s = Page.Request.Form[textboxForLabel.ID];
        string t = Page.Request.Form[textboxForLabel.ClientID];
        textboxForLabel.Text = (string)Page.Request.Form["textboxForLabel"];

        StringBuilder sb = new StringBuilder();
        using (StringWriter sw = new StringWriter(sb))
        {
            using (HtmlTextWriter textWriter = new HtmlTextWriter(sw))
            {
                this.RenderControl(textWriter);
            }
        }
        return sb.ToString();
    }
}

最佳答案

string s = Page.Request.Form[textboxForLabel.UniqueID];

关于c# - 不加载发回的表单值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5585063/

相关文章:

c# - 如何创建自己的表单设计器?

c# - 寻找绘制科学数据的框架 : 2d/3d

c# - 如何在 Visio 2013 加载项中添加自定义面板?

c# - 比较两个复杂对象的最佳方法

javascript - 使用 javascript 分配 session 值

javascript - aspx 控件中的客户端 onkeyup

asp.net - 缩小 ASP.Net MVC 应用程序的 HTML 输出

css - 未应用Docker Asp.Net Core CSS

php - access + mysql 转换为 webplatform = (php + asp.net + mysql)?

c# - 添加多个租户