【问题标题】:Assigning "HTML Select Control" Values in PageLoad() Event在 PageLoad() 事件中分配“HTML 选择控件”值
【发布时间】:2010-01-29 11:31:11
【问题描述】:

我只想在我的页面上使用 HTML 控件。为此,我想使用一些文本框和一个 HTML 选择(列表框)和一个提交事件的提交按钮。 代码如下:

<script type="text/javascript">

    function dropdown(mySel) {
        var myWin, myVal;
        myVal = mySel.options[mySel.selectedIndex].value;
        if (myVal) {
            if (mySel.form.target) myWin = parent[mySel.form.target];
            else myWin = window;
            if (!myWin) return true;
            myWin.location = myVal;
        }
        return false;
    }

</script>

我用于 HTML 选择列表的这个 Java 脚本。 其余代码如下:

<form action="feedback.aspx" method="post" onsubmit="return dropdown(this.feeds)">
<h1>
    Let us know your inconvenience for the following Listing:
</h1>
<table>
    <tr>
        <td>
            <b>Type : </b>
        </td>
        <td>
            <asp:Label ID="lbltype" runat="server"></asp:Label>
        </td>
    </tr>
    <tr>
        <td>
            <b>ID : </b>
        </td>
        <td>
            <asp:Label ID="lblID" runat="server"></asp:Label>
        </td>
    </tr>
    <tr>
        <td>
            <b>Title: </b>
        </td>
        <td>
            <asp:Label ID="lblTitle" runat="server"></asp:Label>
        </td>
    </tr>
</table>
<table>
    <tr>
        <td>
            User ID:
        </td>
        <td>
            <input type="text" name="txtUserid" />
        </td>
    </tr>
    <tr>
        <td>
            Name:
        </td>
        <td>
            <input type="text" name="txtName" />
        </td>
    </tr>
    <tr>
        <td>
            Contact:
        </td>
        <td>
            <input type="text" name="txtContact" />
        </td>
    </tr>
    <tr>
        <td>
            Email:
        </td>
        <td>
            <input type="text" name="txtemail" />
        </td>
    </tr>
</table>
<br />
<table cellpadding="5" cellspacing="5" width="500px">
    <tr>
        <td style="width: 400px">
            <b>Please Select the given option </b>
        </td>
    </tr>
    <tr>
        <td style="width: 400px">
            <select name="feeds">
                <option value="">Choose an Option...</option>
                <option value="">Option1</option>
                <option value="">Option2</option>
                <option value="">Option3</option>
            </select>
        </td>
    </tr>
    <tr>
        <td style="width: 400px">
            <b>Write your Comment Here: </b>
            <br />
            <textarea id="TextArea" name="txtcomment" cols="45" rows="5"></textarea>
        </td>
        <td>
            &nbsp;
        </td>
    </tr>
    <Efextra:Captcha ID="Captcha1" runat="server" TabIndex="29" ValidationErrorMessage="Verification Code"
        ValidationGroup="Feeds" ValidationText="*"></Efextra:Captcha>
    <tr>
        <td>
            <input type="submit" value="Submit FeedBack" validationgroup="Feeds" />
        </td>
    </tr>
</table>
</form>

我有一个具有以下格式的 XML 文档:

<?xml version="1.0" encoding="utf-8" ?>
<Feedback>
<Options>
  <ID>0</ID>
  <option>[Select your option]</option>
 </Options>
 <Options>
  <ID>1</ID>
  <option>I got Incomplete/wrong information posted here.</option>
 </Options>
 <Options>
  <ID>2</ID>
  <option>The Content is not in Proper Format.</option>
 </Options>
 <Options>
  <ID>3</ID>
  <option>How can get the similar type of properties?</option>
 </Options>
 <Options>
  <ID>4</ID>
  <option>This Listing is Sold and Still Displaying here.</option>
 </Options>
 <Options>
  <ID>5</ID>
  <option>I can not send SMS for this Listing.</option>
 </Options>
 <Options>
  <ID>6</ID>
  <option>I can not get the Contact Information of the owner of this post.</option>
 </Options>
 <Options>
  <ID>7</ID>
  <option>My problem is not given here!</option>
 </Options>
</Feedback>

现在我要做的是: 页面加载时会检查用户是否注册,如果他/她是注册用户,则可以自行加载前四个字段[User ID, Name, Contact, Email]。 为此,我的文件后面的代码分配了这样的值:

User FeedUser = Propertywala.User.GetAuthUser();
Txt1.Text=Feeduser.UserID; and so on..

如果他/她不是注册用户,那么用户会将此字段留空,我将通过 Page.Request() 获取所有填充的值。

第二个问题是我想从上面的 XML 文件中加载选择框的选项。 我是这个代码和流程的新手。所以请任何人都可以帮助我这样做。

提前致谢。

【问题讨论】:

    标签: c# javascript html post postback


    【解决方案1】:

    您还需要将runat="server" 属性分配给input type="text" 控件。然后您可以像 web 控件一样在代码隐藏中访问它们。您的select html 控件也是如此。

    对于 Xml 文档,您将需要一个 XmlReader 对象。具体实现请查看MSDN

    【讨论】:

    • OK 其他都还好。我会将 runat="server" 属性放在每个控件中。但是你能通过举个例子告诉我如何将我的 xml 数据绑定到这个选择框吗?
    • 那么 HTML 标签呢?我怎样才能给它赋值? 我希望根据文件后面代码中的逻辑提及此用户。
    • 我不想使用 HTML runat="server"。它应该是完整的 HTML 控件。我该怎么做?
    • 呃,如果你不想使用runat=server,那么XML文档的绑定肯定是我无能为力的。
    【解决方案2】:

    最后我没有使用单个 asp 控件就做到了。我解决了这样的问题:

    <form name="feedback" action="FeedBack.aspx">
    <h1>
        Let us know your inconvenience for the following Listing:
    </h1>
    <table>
        <%if (IsUserLoggedIn) %>
        <%{ %>
        <tr>
            <td>
                <b>User ID:</b>
            </td>
            <td>
                <input type="text" value="<%=VisitingUser.UserIdn%>" size="30" readonly="readonly" />
            </td>
        </tr>
        <tr>
            <td>
                <b>Name:</b>
            </td>
            <td>
                <input type="text" value="<%=VisitingUser.FullName%>" size="30" readonly="readonly" />
            </td>
        </tr>
        <tr>
            <td>
                <b>Contact:</b>
            </td>
            <td>
                <input type="text" value="<%=VisitingUser.MobileNumber %>" size="30" readonly="readonly" />
            </td>
        </tr>
        <tr>
            <td>
                <b>Email:</b>
            </td>
            <td>
                <input type="text" value="<%=VisitingUser.PrimaryEmail%>" size="30" readonly="readonly" />
            </td>
        </tr>
        <%} %>
        <%else%>
        <%{ %><tr>
            <td>
                <h3>
                    Enter the Folowing details to submit the Feedback:
                </h3>
            </td>
        </tr>
        <tr>
            <td>
                Name:
            </td>
            <td>
                <input type="text" name="txtName" />
            </td>
        </tr>
        <tr>
            <td>
                Contact:
            </td>
            <td>
                <input type="text" name="txtContact" />
            </td>
        </tr>
        <tr>
            <td>
                Email:
            </td>
            <td>
                <input type="text" name="txtEmail" />
            </td>
        </tr>
        <%} %>
    </table>
    <br />
    <%if (int.TryParse(Request.QueryString["property"], out Property)) %>
    <%{ %>
    <%Propertywala.Property prp = new Propertywala.Property(Property);%>
    <%if (prp.OwnerId > 0) %>
    <%{ %>
    <table>
        <tr>
            <td>
                <b>Property ID :</b>
            </td>
            <td>
                <%=Property.ToString() %>
            </td>
        </tr>
        <tr>
            <td>
                <b>Property Title :</b>
            </td>
            <td>
                <%=prp.AutoGeneratedTitle %>
            </td>
        </tr>
    </table>
    <%} %>
    <%} %>
    <%else if (int.TryParse(Request.QueryString["project"], out Project)) %>
    <%{ %>
    <%Propertywala.Project prj = new Propertywala.Project(Project);%>
    <%if (prj.UserId > 0) %>
    <%{ %><table>
        <tr>
            <td>
                <b>Project ID : </b>
            </td>
            <td>
                <%=Project.ToString() %>
            </td>
        </tr>
        <tr>
            <td>
                <b>Project Title : </b>
            </td>
            <td>
                <%=prj.Title %>
            </td>
        </tr>
    </table>
    <%} %>
    <%} %>
    <table cellpadding="5" cellspacing="5" width="500px">
        <tr>
            <td style="width: 400px">
                <b>Please Select the given option </b>
            </td>
        </tr>
        <tr>
            <td style="width: 400px">
                <select name="feeds">
                    <%foreach (System.Xml.XmlNode xNode in FeedXmlList)%>
                    <%{ %>
                    <option value="<%=xNode.ChildNodes[1].InnerText%>">
                        <%=xNode.ChildNodes[1].InnerText%></option>
                    <%} %>
                </select>
            </td>
        </tr>
        <tr>
            <td style="width: 400px">
                <b>Write your Comment Here: </b>
                <br />
                <textarea name="txtcomment" cols="45" rows="5"></textarea>
            </td>
            <td>
                &nbsp;
            </td>
        </tr>
        <Efextra:Captcha ID="Captcha1" runat="server" TabIndex="29" ValidationErrorMessage="Verification Code"
            ValidationGroup="Feeds" ValidationText="*"></Efextra:Captcha>
        <tr>
            <td>
                <input type="submit" value="Submit FeedBack" />
            </td>
        </tr>
    </table>
    </form>
    

    最后在代码隐藏文件中,我只是放置了一些必需的条件并通过 request.form 获取所有 html 值,如下所示:

    if (Request.HttpMethod.ToLower() == "post" && Request.Form.Count > 0)
        {
            if (!string.IsNullOrEmpty(Request.Form["feeds"]) || !string.IsNullOrEmpty(Request.Form["txtcomment"]))
            {
                if (!string.IsNullOrEmpty(Request["property"]) || !string.IsNullOrEmpty(Request["project"]))
                {
                    if (IsUserLoggedIn||(!IsUserLoggedIn && !string.IsNullOrEmpty(Request["txtName"]) && !string.IsNullOrEmpty(Request["txtContact"]) && !string.IsNullOrEmpty(Request["txtEmail"])))
                    {
                        if (Request.Form["feeds"] != "[Select your option]")
                        {
                            Mail(Request.Form["feeds"], Request.Form["txtcomment"]);
                        }
                        else
                        {
                            Common.ClientMessage("Select the Option and Retry to Submit the feeds.", CommonConsts.AlertType.info);
    
                        }
                    }
                    else
                    {
                        Common.ClientMessage("Unregistered user must fill their name and Contact Details!", CommonConsts.AlertType.info);
                    }
                }
            }
    
        }
    

    【讨论】:

    • 有人有更好的办法来优化这段代码吗?欢迎任何评论。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-03
    • 2016-12-16
    • 2011-04-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多