简体   繁体   English

使用JavaScript在代码隐藏中设置的输入值

[英]Input Value set with javascript retrieve in codebehind

So I have a repeater that creates <li> elements. 所以我有一个创建<li>元素的转发器。 This is the codebehind for the OnItemDataBound 这是OnItemDataBound

var categoryList = (ProductCategoryObject) e.Item.DataItem;
            var category = ((HtmlAnchor) e.Item.FindControl("category"));
            category.HRef = "javascript:void(0);";
            category.InnerText = categoryList.Name;
            category.Attributes.Add("onclick", "javascript:$('#ProductCategory').val('" + categoryList.Id + "');$('button.product-categories span').text('" + categoryList.Name + "');");

This sets the value of an input of type hidden and now my question is how do I get that value on button click? 这设置了隐藏类型的输入的值,现在我的问题是如何在单击按钮时获得该值? It appears to disappear on postback and I've tried Request["ProductCategory"] and Request.Form["ProductCategory"] 它似乎在回发时消失了,并且我尝试了Request["ProductCategory"]Request.Form["ProductCategory"]

Here is the markup: 这是标记:

<div class="input-group margin-bottom-30">
    <div id="divCategories" runat="server" class="input-group-btn">
        <button type="button" class="btn blue dropdown-toggle  product-categories" data-toggle ="dropdown">
            <asp:Label runat="server" ID="lblCategory">All Categories</asp:Label> <i class="icon-angle-down"></i>
        </button>
        <ul runat="server" ID="ulCategory" class="dropdown-menu">
            <asp:Repeater runat="server" id="rptCategories" OnItemDataBound="CategoriesOnItemDataBound">
                <ItemTemplate>
                    <li>
                        <a runat="server" id="category"></a>
                    </li>
                </ItemTemplate>
            </asp:Repeater>
        </ul>
        <input id="ProductCategory" type="hidden" />
    </div>
    <asp:TextBox runat="server" ID="txtSearch" CssClass="form-control"></asp:TextBox>
    <div class="input-group-btn">
        <asp:LinkButton ID="btnFilter" runat="server" CausesValidation="True" ValidationGroup="BaseValidationGroup" CssClass="btn green" OnClick="SearchBtnOnClick"> Search <i class="icon-search"></i> </asp:LinkButton>
    </div>
</div>

So I figured it out, I had to change my the markup for my input control from 所以我想通了,我必须将输入控件的标记从

<input id="ProductCategory" type="hidden" />

to

<input id="ProductCategory" name="ProductCategory" type="hidden" />

So, essentially, I was missing the name tag which is needed for Request.Form["ProductCategory"] to work. 因此,从本质上讲,我缺少Request.Form["ProductCategory"]工作所需的name标签。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM