将参数传递给转发器内的用户控件

时间:2011-07-12 16:31:20

标签: asp.net user-controls

我有一个转发器列出数据,包括我传递给用户控件的ID,直到我点击链接按钮才能看到该ID。但是,当我点击链接按钮时,ID已丢失。

任何人都知道我哪里出错了?

以下是转发器的代码:

<asp:Repeater ID="uxPolicyList" runat="server" onitemdatabound="uxPolicyList_ItemDataBound">
        <ItemTemplate>
                    <tr>
                        <td class="smaller-ctrl">
                            <img src="Assets/Images/home-icon.gif" alt="Home Insurance Policy" /><%#Eval("PolicyNumber")%>
                        </td>
                        <td>
                            <%#Eval("StartDate","{0:d}")%>
                        </td>
                        <td class="center-cell-ctrl">
                            <%#Eval("Renewal", "{0:d}")%>
                        </td>
                        <td class="center-cell-ctrl">
                            Postcode:<br />
                            <%#Eval("Postcode")%>
                        </td>
                        <td id='<%#Eval("PolicyNumber")%>' class="button-cell">
                            <asp:LinkButton ID="uxPolicySummaryButton" CssClass="policy-link-ctrl" CommandName="PolicyNumber" CommandArgument='<%#Eval("PolicyNumber")%>' OnCommand="uxPolicySummaryButton_Command" runat="server">Policy summary<br /></asp:LinkButton>

                        </td>
                    </tr>
                    <uc1:PolicySummary ID="uxPolicySummary" PolicyNumber='<%#Eval("PolicyNumber")%>' runat="server" Visible="false" />
        </ItemTemplate>

我在用户控件代码隐藏中有这些get / set访问器,但它们总是空白的:

public string _policyNumber = string.Empty;

    public string PolicyNumber
    {
        get
        {
            return _policyNumber;
        }
        set
        {
            _policyNumber = value;
        }
    }

测试时,我在用户控件中硬编码了一个值。即。

<uc1:PolicySummary ID="uxPolicySummary" PolicyNumber="545545-5454-54545" runat="server" Visible="false" />

......它运作良好。任何想法赞赏

2 个答案:

答案 0 :(得分:2)

尝试重新分配PolicyNumber事件中OnCommand属性的值CommandArgument

修改

或将其保存到控件内的HiddenField

public string PolicyNumber
{
    get
    {
        return MyHiddenField.Value;
    }
    set
    {
        MyHiddenField.Value = value;
    }
}

答案 1 :(得分:1)

我认为您可能缺少ViewState后备存储。

public string PolicyNumber     
{         
  get         {             return ViewState["policyNumber"] as string;         }         
  set         {             ViewState["policyNumber"] = value;         }    
}