c#.net 表单回发不保留 1 值

发布于 2024-12-27 20:24:03 字数 8549 浏览 3 评论 0原文

关于我创建的表单的回发有一个奇怪的问题。答案可能非常简单,但我似乎看不到。

我有一个表格,如果视频所在的页面不起作用,用户可以填写该表格。它根据当前选择的视频预先填充字段,并允许用户填写其他字段,并向我发送电子邮件以获取支持。

问题 这些字段已正确预填充,但其中一个字段“页面”虽然已正确预填充,但未将值传递给按钮提交方法。

客户端代码 (包括一些 mootools javascript,这有效)

<asp:Panel ID="pnlVideoProblem" runat="server" Visible="false">
        <h2>Report a video/learning tool issue</h2>
        <div class="keyline"></div>
        <fieldset class="emailform">
            <ul>
                <li><label>Name <span class="error">*</span></label><asp:TextBox ID="txtVideoName" runat="server" MaxLength="60"></asp:TextBox>
                <asp:RequiredFieldValidator ID="RequiredFieldValidator33" runat="server" CssClass="error" ControlToValidate="txtVideoName" ErrorMessage="Required"></asp:RequiredFieldValidator></li>
                <li><label>Email <span class="error">*</span></label><asp:TextBox ID="txtVideoEmail" runat="server" MaxLength="100"></asp:TextBox>
                <asp:RequiredFieldValidator ID="RequiredFieldValidator35" runat="server" CssClass="error" ControlToValidate="txtVideoEmail" ErrorMessage="Required"></asp:RequiredFieldValidator>
                <asp:RegularExpressionValidator ID="RegularExpressionValidator7" runat="server" ControlToValidate="txtVideoEmail" Text="Invalid email" ErrorMessage="Email address is not in the correct format" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"></asp:RegularExpressionValidator></li>
                <li><label>OTHER FIELD</label><asp:TextBox ID="txtOtherField" runat="server" MaxLength="10"></asp:TextBox></li>
                <li><label>Video ID</label><asp:TextBox ID="txtVideoID" runat="server" ReadOnly="true"></asp:TextBox></li>
                <li><label>Browser/Version </label><asp:TextBox ID="txtVideoBrowser" runat="server" MaxLength="100"></asp:TextBox></li>
                <li><label>Flash Version </label><asp:TextBox ID="txtFlashVersion" runat="server"></asp:TextBox></li>
                <li><label>Page </label><asp:TextBox ID="txtVideoPage" runat="server"></asp:TextBox></li>
                <li><label>Visible error messages </label><asp:TextBox ID="txtVisError" runat="server" TextMode="MultiLine" Rows="6" MaxLength="4000"></asp:TextBox></li>

            </ul>
            <asp:Button ID="btnSubmitVideoIssue" runat="server" CssClass="subbutton" Text="Submit report" OnClick="btnSubmitVideoIssue_Click" />
        </fieldset>
        <script type="text/javascript">
            window.addEvent('domready', function () {
                document.id('<%= txtVideoBrowser.ClientID %>').set('value', Browser.Platform.name + ", " + Browser.name + " " + Browser.version);
                document.id('<%= txtFlashVersion.ClientID %>').set('value', Browser.Plugins.Flash.version + "." + Browser.Plugins.Flash.build);
            });
        </script>
    </asp:Panel>

按钮的页面隐藏代码 (回发时不会重置值)

protected void btnSubmitVideoIssue_Click(object sender, EventArgs e)
{
    if (CheckEmptyCaptcha() == false)
    {
        //this field is hidden in css and empty. if it has been filled in, then an automated way of entering has been used.
        //ignore and send no email.
    }
    else
    {
        StringBuilder sbMessage = new StringBuilder();

        emailForm = new MailMessage();

        sbMessage.Append("Name : " + txtVideoName.Text.Trim() + "<br>");
        sbMessage.Append("Email : " + txtVideoEmail.Text.Trim() + "<br>");
        sbMessage.Append("Other Field : " + txtOtherField.Text.Trim() + "<br>");
        sbMessage.Append("Video ID : " + txtVideoID.Text.Trim() + "<br>");
        sbMessage.Append("Browser : " + txtVideoBrowser.Text.Trim() + "<br>");
        sbMessage.Append("Flash Version : " + txtFlashVersion.Text.Trim() + "<br>");
        sbMessage.Append("Visible error messages : " + txtVisError.Text.Trim() + "<br>");
        sbMessage.Append("Url referrer : " + txtVideoPage.Text.Trim()+"<br>");
        sbMessage.Append("Browser : " + Request.UserAgent + "<br>");

        if (txtVideoBrowser.Text.Contains("ie 6"))
        {
            sbMessage.Append("<strong>Browser note</strong> : The PC that made this request looks like it was using Internet Explorer 6, although videos work in IE6, the browser isn't stable software, and therefore Javascript errors may occur preventing the viewing of the page/video/learning tool how it was intended. Recommend that the user upgrades their browsers to the latest version of IE.<br>");
        }
        Double flashver = 0.0;
        if(Double.TryParse(txtFlashVersion.Text, out flashver))
        {
            if(flashver < 9.0)
            {
                sbMessage.Append("<strong>Flash version note</strong> : The PC that made this request is currently using flash version "+flashver+". Flash version 9 or greater is required to view videos. Recommend user upgrades their flash version by visiting http://get.adobe.com/flashplayer<br>");
            }
        }
        else
        {
            sbMessage.Append("<strong>Flash version note</strong> : It doesn't look like flash is installed on the PC that made this request. Flash is required to view videos . Recommend user installs flash by visiting http://get.adobe.com/flashplayer<br>");
        }

        emailForm.To.Add(new MailAddress("[email protected]"));
        emailForm.From = new MailAddress(txtVideoEmail.Text.Trim(), txtVideoName.Text.Trim());
        emailForm.Subject = "[ERROR] - [VIDEO ISSUE] from " + txtVideoName.Text.Trim();
        emailForm.Body = sbMessage.ToString();

        emailForm.IsBodyHtml = true;
        bool sendSuccess = false;

        try
        {
            SmtpClient smtp = new SmtpClient();
            smtp.Send(emailForm);
            sendSuccess = true;
        }
        catch
        {
            pnlVideoProblem.Visible = false;
            pnlFailure.Visible = true;
            ltlFailure.Text = "There was a problem sending your feedback, please go back and try again.";
        }
        finally
        {
            if (sendSuccess)
            {
                pnlVideoProblem.Visible = false;
                pnlSuccess.Visible = true;
                ltlSuccess.Text = "Thank you, your feedback has been sent. Click close to return to the website.";
            }
            else
            {
                pnlVideoProblem.Visible = false;
                pnlFailure.Visible = true;
                ltlFailure.Text = "There was a problem sending your feedback, please go back and try again.";
            }
        }
    }
}

表单值

Name : User
Email : [email protected]
Other Field : aab123
Video Learning ID : 5546
Browser version : win, firefox 9
Flash version : 11.102
Page : https://www.awebsite.com/library/video/5546
Visible error messages : ewrwerwe

生成的电子邮件

Name : User
Email : [email protected]
Other Field : aab123
Video ID : 5546
Browser : win, firefox 9
Flash Version : 11.102
Url referrer : 
Visible error messages : ewrwerwe

视频 ID 和页面/URL 引用地址填充在 (!IsPostBack)

(! IsPostBack)

pnlVideoProblem.Visible = true;
if (!String.IsNullOrEmpty(Request.QueryString["vid"]))
{
    txtVideoID.Text = Request.QueryString["vid"];
}

if (!String.IsNullOrEmpty(Request.QueryString["other"]))
{
    txtOtherField.Text = Request.QueryString["other"];
    txtOtherField.ReadOnly = true;
}
txtVideoPage.Text = HttpUtility.UrlDecode(Request.QueryString["ref"]);
txtVideoPage.ReadOnly = true;

有什么想法吗?我有一堵砖墙,根据答案的简单程度,我可以用头撞墙。

Having a strange problem regarding the postback of a form I've created. The answer will probably be really simple, but I can't seem to see it.

I have a form that a user can fill in if the page a video is on, isn't working. It pre-populates fields based on the current video selected, and allows the user to fill in other fields, and send the email to me for support.

The problem
The fields are pre-populated correctly, but one of the fields 'Page', although pre-populated correctly, doesn't pass the value to the button submit method.

the clientside code
(includes some mootools javascript, this works)

<asp:Panel ID="pnlVideoProblem" runat="server" Visible="false">
        <h2>Report a video/learning tool issue</h2>
        <div class="keyline"></div>
        <fieldset class="emailform">
            <ul>
                <li><label>Name <span class="error">*</span></label><asp:TextBox ID="txtVideoName" runat="server" MaxLength="60"></asp:TextBox>
                <asp:RequiredFieldValidator ID="RequiredFieldValidator33" runat="server" CssClass="error" ControlToValidate="txtVideoName" ErrorMessage="Required"></asp:RequiredFieldValidator></li>
                <li><label>Email <span class="error">*</span></label><asp:TextBox ID="txtVideoEmail" runat="server" MaxLength="100"></asp:TextBox>
                <asp:RequiredFieldValidator ID="RequiredFieldValidator35" runat="server" CssClass="error" ControlToValidate="txtVideoEmail" ErrorMessage="Required"></asp:RequiredFieldValidator>
                <asp:RegularExpressionValidator ID="RegularExpressionValidator7" runat="server" ControlToValidate="txtVideoEmail" Text="Invalid email" ErrorMessage="Email address is not in the correct format" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"></asp:RegularExpressionValidator></li>
                <li><label>OTHER FIELD</label><asp:TextBox ID="txtOtherField" runat="server" MaxLength="10"></asp:TextBox></li>
                <li><label>Video ID</label><asp:TextBox ID="txtVideoID" runat="server" ReadOnly="true"></asp:TextBox></li>
                <li><label>Browser/Version </label><asp:TextBox ID="txtVideoBrowser" runat="server" MaxLength="100"></asp:TextBox></li>
                <li><label>Flash Version </label><asp:TextBox ID="txtFlashVersion" runat="server"></asp:TextBox></li>
                <li><label>Page </label><asp:TextBox ID="txtVideoPage" runat="server"></asp:TextBox></li>
                <li><label>Visible error messages </label><asp:TextBox ID="txtVisError" runat="server" TextMode="MultiLine" Rows="6" MaxLength="4000"></asp:TextBox></li>

            </ul>
            <asp:Button ID="btnSubmitVideoIssue" runat="server" CssClass="subbutton" Text="Submit report" OnClick="btnSubmitVideoIssue_Click" />
        </fieldset>
        <script type="text/javascript">
            window.addEvent('domready', function () {
                document.id('<%= txtVideoBrowser.ClientID %>').set('value', Browser.Platform.name + ", " + Browser.name + " " + Browser.version);
                document.id('<%= txtFlashVersion.ClientID %>').set('value', Browser.Plugins.Flash.version + "." + Browser.Plugins.Flash.build);
            });
        </script>
    </asp:Panel>

the page-behind code for the button
(there is no reseting of the values on postback)

protected void btnSubmitVideoIssue_Click(object sender, EventArgs e)
{
    if (CheckEmptyCaptcha() == false)
    {
        //this field is hidden in css and empty. if it has been filled in, then an automated way of entering has been used.
        //ignore and send no email.
    }
    else
    {
        StringBuilder sbMessage = new StringBuilder();

        emailForm = new MailMessage();

        sbMessage.Append("Name : " + txtVideoName.Text.Trim() + "<br>");
        sbMessage.Append("Email : " + txtVideoEmail.Text.Trim() + "<br>");
        sbMessage.Append("Other Field : " + txtOtherField.Text.Trim() + "<br>");
        sbMessage.Append("Video ID : " + txtVideoID.Text.Trim() + "<br>");
        sbMessage.Append("Browser : " + txtVideoBrowser.Text.Trim() + "<br>");
        sbMessage.Append("Flash Version : " + txtFlashVersion.Text.Trim() + "<br>");
        sbMessage.Append("Visible error messages : " + txtVisError.Text.Trim() + "<br>");
        sbMessage.Append("Url referrer : " + txtVideoPage.Text.Trim()+"<br>");
        sbMessage.Append("Browser : " + Request.UserAgent + "<br>");

        if (txtVideoBrowser.Text.Contains("ie 6"))
        {
            sbMessage.Append("<strong>Browser note</strong> : The PC that made this request looks like it was using Internet Explorer 6, although videos work in IE6, the browser isn't stable software, and therefore Javascript errors may occur preventing the viewing of the page/video/learning tool how it was intended. Recommend that the user upgrades their browsers to the latest version of IE.<br>");
        }
        Double flashver = 0.0;
        if(Double.TryParse(txtFlashVersion.Text, out flashver))
        {
            if(flashver < 9.0)
            {
                sbMessage.Append("<strong>Flash version note</strong> : The PC that made this request is currently using flash version "+flashver+". Flash version 9 or greater is required to view videos. Recommend user upgrades their flash version by visiting http://get.adobe.com/flashplayer<br>");
            }
        }
        else
        {
            sbMessage.Append("<strong>Flash version note</strong> : It doesn't look like flash is installed on the PC that made this request. Flash is required to view videos . Recommend user installs flash by visiting http://get.adobe.com/flashplayer<br>");
        }

        emailForm.To.Add(new MailAddress("[email protected]"));
        emailForm.From = new MailAddress(txtVideoEmail.Text.Trim(), txtVideoName.Text.Trim());
        emailForm.Subject = "[ERROR] - [VIDEO ISSUE] from " + txtVideoName.Text.Trim();
        emailForm.Body = sbMessage.ToString();

        emailForm.IsBodyHtml = true;
        bool sendSuccess = false;

        try
        {
            SmtpClient smtp = new SmtpClient();
            smtp.Send(emailForm);
            sendSuccess = true;
        }
        catch
        {
            pnlVideoProblem.Visible = false;
            pnlFailure.Visible = true;
            ltlFailure.Text = "There was a problem sending your feedback, please go back and try again.";
        }
        finally
        {
            if (sendSuccess)
            {
                pnlVideoProblem.Visible = false;
                pnlSuccess.Visible = true;
                ltlSuccess.Text = "Thank you, your feedback has been sent. Click close to return to the website.";
            }
            else
            {
                pnlVideoProblem.Visible = false;
                pnlFailure.Visible = true;
                ltlFailure.Text = "There was a problem sending your feedback, please go back and try again.";
            }
        }
    }
}

the form values

Name : User
Email : [email protected]
Other Field : aab123
Video Learning ID : 5546
Browser version : win, firefox 9
Flash version : 11.102
Page : https://www.awebsite.com/library/video/5546
Visible error messages : ewrwerwe

the resulting email

Name : User
Email : [email protected]
Other Field : aab123
Video ID : 5546
Browser : win, firefox 9
Flash Version : 11.102
Url referrer : 
Visible error messages : ewrwerwe

Video ID and Page/Url Referrer are populated on (!IsPostBack)

(!IsPostBack)

pnlVideoProblem.Visible = true;
if (!String.IsNullOrEmpty(Request.QueryString["vid"]))
{
    txtVideoID.Text = Request.QueryString["vid"];
}

if (!String.IsNullOrEmpty(Request.QueryString["other"]))
{
    txtOtherField.Text = Request.QueryString["other"];
    txtOtherField.ReadOnly = true;
}
txtVideoPage.Text = HttpUtility.UrlDecode(Request.QueryString["ref"]);
txtVideoPage.ReadOnly = true;

any ideas? I have a brick wall i can hit my head against based on how simple the answer is.

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

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

发布评论

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

评论(2

自在安然 2025-01-03 20:24:03

如果TextBox的ReadOnly属性为“true”,则不会回发数据
已加载,例如,它本质上意味着 TextBox 是只读的
服务器端观点(客户端更改将被忽略)。

如果您希望 TB 以“旧方式”只读,请使用:

TextBox1.Attributes.Add("只读","只读")

因为这不会影响服务器端功能。

来自: http://aspadvice.com/blogs/joteke/ archive/2006/04/12/16409.aspx


另请参阅:http://msdn.microsoft.com/ en-us/library/system.web.ui.webcontrols.textbox.readonly.aspx

If TextBox's ReadOnly property is "true", postback data won't be
loaded e.g it essentially means TextBox being readonly from
server-side standpoint (client-side changes will be ignored).

If you want TB to be readonly in the "old manner" use:

TextBox1.Attributes.Add("readonly","readonly")

as that won't affect server-side functionality.

From: http://aspadvice.com/blogs/joteke/archive/2006/04/12/16409.aspx


See also: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.textbox.readonly.aspx

赤濁 2025-01-03 20:24:03

解决了:)

在一丝阳光之后,我记得我有这个

RewriteRule ^/pop/(.*[^/])/report-issue/([a-fA-F0-9]{32})-([0-9]+)$ /pop.aspx?tp=$1&pg=report-issue&vid=$2&other=$3&ref=%{HTTP_REFERER} [L]

作为重写规则,然后这让我仔细看看表单字段是否实际上被包装在回发中。你瞧,事实并非如此。

我在查询字符串变量读取部分包裹了一个 !postback,它起作用了!

砖墙需要很厚。

感谢那些试图提供帮助的人!

Solved :)

After a tiny ray of sunshine i remembered I had this

RewriteRule ^/pop/(.*[^/])/report-issue/([a-fA-F0-9]{32})-([0-9]+)$ /pop.aspx?tp=$1&pg=report-issue&vid=$2&other=$3&ref=%{HTTP_REFERER} [L]

as a rewrite rule, which then led me to take a closer look at whether the form fieldswere actually was wrapped in a postback. Lo-and-behold, they werent.

I've wrapped a !postback around the querystring variable reading section and it works!

brick wall needs to be very thick.

thanks to those that tried to help!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文