在codebehind中使用javascript检索设置的输入值

时间:2015-05-20 19:36:46

标签: javascript c# jquery html asp.net

所以我有一个创建<li>元素的转发器。这是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 + "');");

这设置了隐藏类型输入的值,现在我的问题是如何在按钮点击时获得该值?它似乎在回发中消失,我已经尝试Request["ProductCategory"]Request.Form["ProductCategory"]

这是标记:

<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>

1 个答案:

答案 0 :(得分:0)

所以我想通了,我必须从

更改输入控件的标记
<input id="ProductCategory" type="hidden" />

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

所以,基本上,我错过了name工作所需的Request.Form["ProductCategory"]标记。