nu everest nu everest - 18 days ago 7
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

btnMode
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"
runat="server"
ClientIDMode="Static"
CssClass='<%# Eval("btnMode") %>'
Text="edit"
CommandName="ChangePaymentProfile"
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";

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


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:

<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))" >


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

Answer

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">
    <ItemTemplate>
        <div>
            <asp:Button ID="TestButton" runat="server" Text='<%# Eval("someText") %>'/>
        </div>
    </ItemTemplate>
</asp:Repeater>

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;
    RepeaterTest.DataBind();
}
protected void RepeaterTest_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    dynamic foo = e.Item.DataItem;
    ((Button)e.Item.FindControl("TestButton")).CssClass = foo.someText;
}
Comments