ASP.. NET将asp:Repeater值传递给asp:Button CssClass属性

本文关键字:asp Button 属性 CssClass Repeater NET ASP 值传 | 更新日期: 2023-09-27 18:17:26

我有一个包含按钮的支付方法中继器。我们有新的按钮样式需要应用。新的按钮样式根据数据库中的btnMode设置进行更改,该设置设置为表示CSS类选择器的字符串。CSS工作正常

我把它放在ASPX页面:

<asp:Button ID="btnEdit" 
            runat="server" 
            ClientIDMode="Static" 
            CssClass='<%# Eval("btnMode") %>' 
            Text="edit" 
            CommandName="ChangePaymentProfile" 
            CommandArgument='<%# Eval("PaymentSourceId") + "|" + Eval("AuthNetPaymentProfileId")%>' />  
在ASPX.cs 中
    //Command Button Clicked: Change Payment Method
    else if (e.CommandName.ToLower().Equals("changepaymentprofile"))
    {
        hdChangeYN.Value = "Y";
        showAddPaymentForm();
        //display billing address of selected card
        hsParams.Add("CustomerId", User.Identity.Name);
        hsParams.Add("PaymentSourceId", strPaymentSourceId);
        DataTable dt = DbHelper.GetDataTableSp("234_accountAddress__ByPaySourceId", hsParams);
        if (dt.Rows.Count > 0)
        {
            tbFistName.Text = dt.Rows[0]["FirstName"].ToObjectString();
            tbLastName.Text = dt.Rows[0]["LastName"].ToObjectString();
            inputAddress1.Text = dt.Rows[0]["Address"].ToObjectString();
            inputAddress2.Text = "";
            string strCountryCd = dt.Rows[0]["CountryCd"].ToObjectString();
            ddlCountry_Update(strCountryCd); //Update Country & State DDL because Country can be a foreign country
            ddlCountry.SelectedValue = strCountryCd;
            inputCity.Text = dt.Rows[0]["City"].ToObjectString();
            ddlState.SelectedValue = dt.Rows[0]["StateProvinceId"].ToObjectString();
            inputZipcode.Text = dt.Rows[0]["Zipcode"].ToObjectString();
            ddlCardType.SelectedValue = dt.Rows[0]["CardType"].ToObjectString();
        }
    }

当我在浏览器中加载页面时,<%# Eval("btnMode") %>没有被解析为一个值。当我打开检查器时,我看到了这个:

<input 
    id="btnEdit" 
    class="<%# Eval("btnMode") %>"
    type="submit" 
    name="ctl00$ctl00$ContentPlaceHolderFront$ContentPlaceHolderFront$rptList$ctl01$btnPrimary" 
    value="" 
    onclick="javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(&quot;ctl00$ctl00$ContentPlaceHolderFront$ContentPlaceHolderFront$rptList$ctl01$btnPrimary&quot;, &quot;&quot;, true, &quot;&quot;, &quot;&quot;, false, false))" >

需要指出的是,CommandArgument='<%# Eval("PaymentSourceId") %>'这个属性是有效的,btnMode包含有效的数据。

ASP.. NET将asp:Repeater值传递给asp:Button CssClass属性

正如我在评论中所写的,并不是Asp中的所有属性。可以绑定网络控件。CssClass是不能被绑定的。

要解决这个问题,可以在Button所在的中继器中添加OnItemDataBound事件处理程序。在事件处理程序中,您可以使用e.Item.DataItem来获取所需的值,并将其设置为按钮上的CssClass。示例代码:

<asp:Repeater ID="RepeaterTest" runat="server" OnItemDataBound="RepeaterTest_ItemDataBound">
    <ItemTemplate>
        <div>
            <asp:Button ID="TestButton" runat="server" Text='<%# Eval("someText") %>'/>
        </div>
    </ItemTemplate>
</asp:Repeater>

和后面的代码:

protected void Page_Load(object sender, EventArgs e)
{
    var testData = Enumerable.Range(1, 10).Select(i => new { someText = "Button " + i.ToString() }).ToList();
    RepeaterTest.DataSource = testData;
    RepeaterTest.DataBind();
}
protected void RepeaterTest_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    dynamic foo = e.Item.DataItem;
    ((Button)e.Item.FindControl("TestButton")).CssClass = foo.someText;
}