【问题标题】:How can I add control clientID to the Request.Form collection while posting back如何在回发时将控件 clientID 添加到 Request.Form 集合
【发布时间】:2011-12-02 20:48:00
【问题描述】:

很快就需要这方面的帮助.....

我有一个 Gridview,其中的列主要是“Itemtemplates”,除了在呈现时为其中的控件提供的客户端 ID 之外,没有为它们分配任何特定的 ID。

我目前正在对现有页面进行更改,gridview 中的控件的客户端 ID 很少出现在 Request.Form 集合中,并且很少出现。 我需要将客户端 ID 添加到 Request.Form 集合中,并在发回服务器时从那里访问它的值。

控件如何/何时添加到 Request.Form 集合中?我对此感到困惑。 任何帮助将不胜感激。

谢谢

将动态列添加到网格的代码:

else if (Mode != ProductGridMode.Search)
    {
        grid.Columns.Add(CreateTemplateField(
            path + "ProductDetailHeader.ascx",
            path + "ProductDetailCell.ascx"));

        grid.Columns.Add(CreateTemplateField(
            path + "CustomerPartNumberHeader.ascx",
            path + "CustomerPartNumberCell.ascx", "td-partnumber"));

        grid.Columns.Add(CreateTemplateField(
            path + "OrderQuantityHeader.ascx",
            path + "OrderQuantityCell.ascx", "td-qty"));

        if (Mode == ProductGridMode.OrderHistoryPayments
            || Mode == ProductGridMode.OrderHistoryPaymentsPrintView)
        {
            grid.Columns.Add(CreateTemplateField(
                path + "AvailableToShipHeader.ascx",
                path + "AvailableToShipCell.ascx"));
        }

        grid.Columns.Add(CreateTemplateField(
            path + "PriceHeader.ascx",
            path + "PriceCell.ascx", "td-price"));

        grid.Columns.Add(CreateTemplateField(
            path + "ExtendedPriceHeader.ascx",
            path + "ExtendedPriceCell.ascx", "td-ext-price"));

        if (Mode == ProductGridMode.OrderHistory || Mode == ProductGridMode.OrderHistoryPrintView
            || Mode == ProductGridMode.OrderHistoryPayments || Mode == ProductGridMode.OrderHistoryPaymentsPrintView)
        {
            grid.Columns.Add(CreateTemplateField(
                path + "InvoiceHeader.ascx",
                path + "InvoiceCell.ascx"));

            grid.EmptyDataTemplate = LoadTemplate(path + "EmptyDataTemplate.ascx");
        }
    }
    else
    {

        grid.Columns.Add(CreateTemplateField(
            path + "ImageHeader.ascx",
            path + "ImageCell.ascx"));

        grid.Columns.Add(CreateTemplateField(
            path + "MouserPartNumberHeader.ascx",
            path + "MouserPartNumberCell.ascx"));

        grid.Columns.Add(CreateTemplateField(
            path + "MfrPartNumberHeader.ascx",
            path + "MfrPartNumberCell.ascx"));

        grid.Columns.Add(CreateTemplateField(
            path + "ManufacturerHeader.ascx",
            path + "ManufacturerCell.ascx"));

        grid.Columns.Add(CreateTemplateField(
            path + "DescriptionHeader.ascx",
            path + "DescriptionCell.ascx"));

        grid.Columns.Add(CreateTemplateField(
            path + "DocumentHeader.ascx",
            path + "DocumentCell.ascx"));

        grid.Columns.Add(CreateTemplateField(
            path + "AvailabilityHeader.ascx",
            path + "AvailabilityCell.ascx"));

        grid.Columns.Add(CreateTemplateField(
            path + "PricingHeader.ascx",
            path + "PricingCell.ascx"));

        grid.Columns.Add(CreateTemplateField(
            path + "QuantityHeader.ascx",
            path + "QuantityCell.ascx"));

        grid.Columns.Add(CreateTemplateField(
            path + "OrderInfoHeader.ascx",
            path + "OrderInfoCell.ascx"));

        grid.Columns.Add(CreateTemplateField(
            path + "RoHSHeader.ascx",
            path + "RoHSCell.ascx"));

        // Don't include Select column in count
        Search_TemplateColumnCount = grid.Columns.Count - CellOffset;

        // Due to their nature, the dynamic columns must be created as late as possible which is
        // right before databinding instead of here.
    }

用于项目模板之一(我关心的那个)中使用的用户控件的 aspx

    <%@ Control Language="C#" AutoEventWireup="true" CodeFile="MfrPartNumberCell.ascx.cs"
    Inherits="Controls_ProductGridTemplates_Search_MfgPartNumberCell"        WhitespaceCleaning="Clean" %>
<div style="text-align: left;">
        <%--<asp:Literal ID="litMfrPartNumber" runat="server" Text='<%# Bind("MfrPartNumber") %>' /><br />--%>
        <asp:HyperLink ID="MfrPartNumberLink" runat="server" Text='<%# Bind("MfrPartNumber") %>'
        OnDataBinding="MfrPartNumberLink_DataBinding" /><br />
    <br />
    <asp:HyperLink ID="lnkCrossReference" runat="server" CssClass="highlight" Style="color: #FF6600"
        Target="_blank" OnDataBinding="lnkCrossReference_DataBinding" />
    <div style="width: 100%; text-align: center;">
        <asp:PlaceHolder ID="PHlifecycle" runat="server"></asp:PlaceHolder>
    </div>
</div>

用户控制代码:

    public partial class Controls_ProductGridTemplates_Search_MfgPartNumberCell : System.Web.UI.UserControl
{
    protected void Page_PreRender(object sender, EventArgs e)
    {
        lnkCrossReference.Visible = (lnkCrossReference.NavigateUrl != string.Empty);
    }

    protected void MfrPartNumberLink_DataBinding(object sender, EventArgs e)
    {
        SearchResultItem searchItem = BusinessHelper.GetDataItem(this) as SearchResultItem;

        MfrPartNumberLink.NavigateUrl = BusinessHelper.BuildProductDetailUrl(searchItem);

        //life cycle
        if (searchItem.LifeCycle != "")
        {
            if (!(searchItem.XXXX== false && searchItem.LifeCycle.ToLower().Trim() == "new at mouser"))
            {
                string lifecycleInfo = Mouser.BOM.ExcelHelper.ProductRules.BuildLifecycleText(searchItem.LifeCycle);
                if (lifecycleInfo != "")
                    PHlifecycle.Controls.Add(new LiteralControl(lifecycleInfo + "<br/><br/>"));
            }
        }

    }

    protected void lnkCrossReference_DataBinding(object sender, EventArgs e)
    {
        SearchResultItem searchItem = BusinessHelper.GetDataItem(this) as SearchResultItem;

        if (!string.IsNullOrEmpty(searchItem.CrossReference))
        {
            StringBuilder crossRefUrlBuilder = new StringBuilder();
            crossRefUrlBuilder.Append("~/Search/CrossPopUp.aspx?xpn=");
            crossRefUrlBuilder.Append(HttpUtility.UrlEncode(searchItem.MfrPartNumber));
            crossRefUrlBuilder.Append("&criteria=");
            crossRefUrlBuilder.Append(HttpUtility.UrlEncode(searchItem.CrossReference));

            lnkCrossReference.NavigateUrl = crossRefUrlBuilder.ToString();
            lnkCrossReference.Attributes.Add("onclick",
                "javascript:window.open('" + ResolveUrl(lnkCrossReference.NavigateUrl)
                + "','CatalogPage', 'height=300,width=400,toolbars=no,menubar=no,location=no');return false;");
            lnkCrossReference.Text = String.Format(Resources.MyCXXXX.lnkCrossReference, searchItem.CrossReference);
        }
    }
}

【问题讨论】:

  • 你能添加你的gridview aspx代码吗?
  • 嗨,..刚刚用一些代码编辑了我的问题...有帮助吗?还是你需要别的东西?

标签: c# javascript asp.net


【解决方案1】:

HTML form 中的任何 HTML input 控件都将包含在 Request.Form 集合中。这些值包含在 POST 操作的 HTTP 标头中。

HTTP Headers for Dummies

编辑:添加代码

default.aspx

<%@ Page Title="Home Page" Trace="true" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
    CodeBehind="Default.aspx.cs" Inherits="SO8362448._Default" %>

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent"> </asp:Content> <asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    <asp:GridView ID="gridView" runat="server" OnRowDataBound="gridView_rowDataBound" AutoGenerateColumns="false">
        <Columns>
            <asp:TemplateField>
                <ItemTemplate>
                    <asp:Literal runat="server" Text='<%# Eval("Name") %>' />
                    <asp:LinkButton id="hyperLink" runat="server" Text="Click Me" />
                    <asp:HiddenField ID="hiddenField" runat="server" />
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView> </asp:Content>

default.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SO8362448
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            gridView.DataSource = new List<Person>
            {
                new Person {Name = "Steve", Age = 21},
                new Person {Name = "Cindy", Age = 34}
            };
            gridView.DataBind();
        }

        protected void gridView_rowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                LinkButton lb = e.Row.FindControl("hyperLink") as LinkButton;
                if (lb != null)
                {
                    HiddenField hf = e.Row.FindControl("hiddenField") as HiddenField;
                    if (hf != null)
                    {
                        hf.Value = lb.Text;
                    }
                }
            }
        }
    }
}

【讨论】:

  • 假设我正在尝试在 gridview 中获取“超链接”字段的“文本”...如何将其附加到请求中?
  • @karry - 添加隐藏类型的输入字段并用超链接的文本填充它。这可以在服务器端完成,也可以在客户端使用 jQuery。
  • 再次感谢戴夫..这对我有帮助:)
猜你喜欢
  • 2012-11-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-10-27
  • 2013-03-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多