nu everest nu everest - 1 year ago 86
ASP.NET (C#) Question

ASP.NET Passing asp:Repeater value to asp:Button CssClass attribute

I have a payment method repeater that contains a button. We have new button styles that need to be applied. The new button style changes based on a

setting in the database which is set to a string representing a CSS class selector. The CSS works fine.

I put this in the ASPX page:

<asp:Button ID="btnEdit"
CssClass='<%# Eval("btnMode") %>'
CommandArgument='<%# Eval("PaymentSourceId") + "|" + Eval("AuthNetPaymentProfileId")%>' />

In the ASPX.cs

//Command Button Clicked: Change Payment Method
else if (e.CommandName.ToLower().Equals("changepaymentprofile"))
hdChangeYN.Value = "Y";


//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();

When I load the page in the browser the
<%# Eval("btnMode") %>
does not get resolved to a value. I see this when I open the inspector:

class="<%# Eval("btnMode") %>"
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))" >

It is important to point out that this attribute
CommandArgument='<%# Eval("PaymentSourceId") %>'
does work, and
does contain valid data.

Answer Source

As I wrote in a comment, not all properties in Asp.Net controls can be databound. CssClass is one that cannot be databound.

To get around this, you can add an OnItemDataBound event handler to the repeater where the Button is. In the event handler you can then user the e.Item.DataItem to get the value you want and set it as the CssClass on the button. Sample code:

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

and code behind:

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;
protected void RepeaterTest_ItemDataBound(object sender, RepeaterItemEventArgs e)
    dynamic foo = e.Item.DataItem;
    ((Button)e.Item.FindControl("TestButton")).CssClass = foo.someText;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download