Reputation: 1
this what my datalist looks like this is the datalist
I tried to cast the table of the HTML as a web control table and then access it style from code behind and change it like I did with the buttons the problem is that the table border wont change
Upvotes: 0
Views: 717
Reputation: 49329
You as a general rule can use the row data bound event.
Say this markup:
<asp:DataList ID="DataList1" runat="server" DataKeyField="ID" OnItemDataBound="DataList1_ItemDataBound" >
<ItemTemplate>
<div id="myrow" runat="server" style="border-style:solid;color:black;width:300px;">
<div style="padding:5px;text-align:right">
<p>Hotel Name: <asp:TextBox ID="HotelName" runat="server" Text ='<%# Eval("HotelName") %>' /></p>
<p>First Name: <asp:TextBox ID="FirstName" runat="server" Text ='<%# Eval("FirstName") %>' /></p>
<p>Last Name: <asp:TextBox ID="LastName" runat="server" Text ='<%# Eval("LastName") %>' /></p>
<p>City: <asp:TextBox ID="City" runat="server" Text ='<%# Eval("City") %>' /></p>
<p>Province: <asp:TextBox ID="Province" runat="server" Text ='<%# Eval("Province") %>' /></p>
Active: <asp:CheckBox ID="Active" runat="server" Checked = '<%# Eval("Active") %>'/>
</div>
</div>
</ItemTemplate>
</asp:DataList>
And now code behind can look like this:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
LoadData();
void LoadData()
{
using (SqlConnection conn = new SqlConnection(Properties.Settings.Default.TEST4))
{
string strSQL = "SELECT top 10 * from tblHotels ORDER BY HotelName";
using (SqlCommand cmdSQL = new SqlCommand(strSQL, conn))
{
conn.Open();
DataTable rstData = new DataTable();
rstData.Load(cmdSQL.ExecuteReader());
DataList1.DataSource = rstData;
DataList1.DataBind();
}
}
}
And data bound, we can use for formatting any control/text box. Say turn City red for city = "Edmonton"
We have this:
protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item){
TextBox txtCity = e.Item.FindControl("City") as TextBox;
if (txtCity.Text == "Edmonton")
{
// set border as red
txtCity.BorderColor = System.Drawing.Color.FromName("red");
}
}
}
results:
Upvotes: 0
Reputation: 44
you should set the atribute runat="server"
to the item you want to change from code behind.
and in the code behind yo can try something like this:
label4.Attributes.Add('bgcolor', 'red');
where labe4 is the id of the element you want to change
Upvotes: 0