【问题标题】:ASP.NET Passing asp:Repeater value to asp:Button CssClass attributeASP.NET 将 asp:Repeater 值传递给 asp:Button CssClass 属性
【发布时间】:2016-11-15 23:04:08
【问题描述】:

我有一个包含按钮的付款方式中继器。我们有需要应用的新按钮样式。新按钮样式根据数据库中的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();
        }
    }

当我在浏览器中加载页面时,&lt;%# Eval("btnMode") %&gt; 不会被解析为一个值。当我打开检查器时,我看到了这一点:

<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='&lt;%# Eval("PaymentSourceId") %&gt;' 这个属性确实有效,btnMode 确实包含有效数据。

【问题讨论】:

  • 请在代码后面显示您的btnPrimary绑定方法,可能您在代码级别存在数据绑定问题,影响页面生命周期。
  • 您正在显示 2 个不同的按钮。 btnEdit的aspx和btnPrimary的输出html。是哪个?
  • btnEdit。我实际上有三个按钮需要以这种方式设置。问题已解决。
  • 我试过你的asp:Buttonsn-p。它按应有的方式工作。我在 HTML 中得到了 Eval 等价的类名,没有 onclick 事件。 onclick 回发功能从哪里来的 type="submit" 按钮?
  • 并非所有 Asp.Net 控件属性都是可数据绑定的。只有标有BindableAttribute 的属性才能进行数据绑定。 CssClass 似乎不是其中之一。见BindableAttributeCssClass

标签: c# asp.net button webforms


【解决方案1】:

正如我在评论中所写,并非 Asp.Net 控件中的所有属性都可以数据绑定。 CssClass 是不能数据绑定的。

要解决这个问题,您可以将OnItemDataBound 事件处理程序添加到Button 所在的转发器。在事件处理程序中,您可以使用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;
}

【讨论】:

  • 这对我有帮助,但 e.Item.DataItem.IsPrimaryDisplay 对我不起作用。而是DataBinder.Eval(e.Item.DataItem, "IsPrimaryDisplay").ToString()。即这个方法stackoverflow.com/a/1471443/1783439
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-11-23
  • 1970-01-01
  • 1970-01-01
  • 2013-07-02
  • 1970-01-01
  • 2012-11-10
  • 1970-01-01
相关资源
最近更新 更多