For some reason in the C# code behind for asp.net, i cannot call the table by it's ID to set it's back color property. I tried and and nothing seems to work
Here is my asp.net table tag:
<table id="ptbl" runat="server" cellpadding="2" width="640px" border="1">
here is something similar to what i what to do in the C# code behind but it does not recognize the id
ptbl.Attributes.Add("style", "background-color:red")");
Any ideas/suggestions?
Update: here is the code. There is a layout template in it so somehow it can't see the table id, but if I take that out of it then it sees it. What can i do. I need the listview to get the data
<asp:ListView ID="ListView1" runat="server" Style="color: white; font-weight: bold">
<LayoutTemplate>
<table id="ptbl" runat="server" cellpadding="2" width="640px" border="1" style="color: black; font-weight: bold">
<tr runat="server">
<th runat="server">Ps</th>
<th runat="server">P</th>
<th runat="server">T</th>
</tr>
<tr runat="server" id="itemPlaceholder" />
</table>
<asp:DataPager runat="server" ID="ContactsDataPager" PageSize="90">
<Fields>
<asp:NextPreviousPagerField ShowFirstPageButton="true" ShowLastPageButton="true"
FirstPageText="|<< " LastPageText=" >>|"
NextPageText=" > " PreviousPageText=" < " />
</Fields>
</asp:DataPager>
</LayoutTemplate>
<ItemTemplate>
<tr runat="server">
<td>
<asp:Label ID="Label1" runat="server"><%# Eval("Ps") %></asp:Label></td>
<td>
<asp:Label ID="Label2" runat="server"><%# Eval("P") %></asp:Label></td>
<td>
<asp:Label ID="Label3" runat="server"><%# Eval("T") %></asp:Label></td>
</tr>
</ItemTemplate>
</asp:ListView>
Again the layout template is causing the issue. how can i use the listview and the table with the layout template. I know this has morphed a bit now , but any help would be appreciated.
You cannot access the ptbl table until DataBind() has been called on ListView1. After it has been called and there is data in the dataset, then you can reference the the ptbl table like this:
protected void Page_Load(object sender, EventArgs e)
{
var tbl = new DataTable();
tbl.Columns.Add("Ps", typeof(Int32));
tbl.Columns.Add("P", typeof(string));
tbl.Columns.Add("T", typeof(string));
var r = tbl.NewRow();
r[0] = 99;
r[1] = "Hey";
r[2] = "USA";
tbl.Rows.Add(r);
ListView1.DataSource = tbl;
ListView1.DataBind();
var ptbl = (HtmlTable)ListView1.FindControl("ptbl");
ptbl.Style.Add("background-color", "red");
}
Related
I need to hide table rows in a DataList if column data returns null from SQL Server (for each individual column). I have it working successfully but this method will be very tedious as I have about 100 rows in my table. Surely there is a simpler way.
Here is my C# code:
protected void DataList1_ItemDataBound1(object sender, DataListItemEventArgs e)
{
if ((String.IsNullOrEmpty(((Label)e.Item.FindControl("lblAccountStatus")).Text)))
{
HtmlTableRow row = (HtmlTableRow)e.Item.FindControl("rowAccountStatus");
row.Visible = false;
}
if ((String.IsNullOrEmpty(((Label)e.Item.FindControl("lblAccountName")).Text)))
{
HtmlTableRow row = (HtmlTableRow)e.Item.FindControl("rowAccountName");
row.Visible = false;
}
}
Here is my webform markup:
<asp:DataList ID="DataListAccount" runat="server" OnItemDataBound="DataList1_ItemDataBound1">
<ItemTemplate>
<tr>
<td style="width: 171px">Account Status:</td>
<td style="width: 220px">
<asp:Label ID="lblAccountStatus" runat="server" Text='<%# Eval("ACCOUNT_STATUS") %>'></asp:Label>
</td>
</tr>
<tr id="rowAccountName">
<td style="width: 171px">Account Status:</td>
<td style="width: 220px">
<asp:Label ID="lblAccountName" runat="server" Text='<%# Eval("ACCOUNT_NAME") %>'></asp:Label>
</td>
</tr>
</ItemTemplate>
</asp:DataList>
You can wrap the ItemTemplate contents with a PlaceHolder and use a Ternary Operator to set the visibility.
<asp:PlaceHolder ID="PlaceHolder1" runat="server" Visible='<%# !string.IsNullOrEmpty(Eval("ACCOUNT_STATUS").ToString()) ? true : false %>'>
<tr>
<td style="width: 171px">Account Status:</td>
<td style="width: 220px">
<asp:Label ID="lblAccountStatus" runat="server" Text='<%# Eval("ACCOUNT_STATUS") %>'></asp:Label>
</td>
</tr>
</asp:PlaceHolder>
But I would recommend you make sure you filter the source data of empty items. Something like
SELECT * FROM accounts WHERE account_status IS NOT NULL
just modify this code by adding multiple conditions
string value = Convert.ToString( row["MyColumn"]);
if (string.IsNullOrEmpty(value))
I was coding for my project which I am working on currently and had the need to
Add courses and batch to courses
Delete courses on demand
using gridview for the same.
I am using OnRowCommand event, the deletion works fine,entry is being deleted from database too, but for some reason, this error is being thrown:
"The GridView 'gvCourses' fired event RowDeleting which wasn't handled."
Note:- I am not using 'OnRowDeleting' in the aspx file.
Here is the odd thing which I, as a beginner do not understand. The error does not show up anymore when I generate the 'OnRowDeleting' event and leave the event blank in code behind behind(0 lines of code inside the event handler).
Looking to learn and understand why it is happening. Any help would be greatly appreciated.
.aspx code:
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<asp:Panel ID="Panel2" runat="server">
<table style="width:100%;">
<tr>
<td style="width:210px">
</td>
<td>
</td>
</tr>
<tr>
<td style="width:210px"> </td>
<td> </td>
</tr>
<tr>
<td style="width:210px; height: 331px;"></td>
<td style="height: 331px">
<asp:GridView ID="gvCourses" runat="server"
AutoGenerateColumns="False"
CssClass="table-hover table"
GridLines="None" Width="800px"
ShowFooter="True"
OnRowCommand="gvCourses_RowCommand" >
<Columns>
<asp:BoundField DataField="course"
HeaderText="Courses in Valsura"
SortExpression="DateField" />
<asp:TemplateField>
<FooterTemplate>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem>Select</asp:ListItem>
<asp:ListItem>Students</asp:ListItem>
<asp:ListItem>Teachers</asp:ListItem>
</asp:DropDownList>
<asp:Button ID="Button1" runat="server"
OnClick="Button1_Click"
Text="Add Course"
CssClass="btn-danger" />
</FooterTemplate>
<ItemTemplate>
<asp:LinkButton ID="LinkButton2"
runat="server">View Batches</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="LinkButton3"
runat="server"
CommandName="delete"
CommandArgument='<%#Eval ("course") %>'>Delete</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:RequiredFieldValidator ID="rfvCourse"
ControlToValidate="DropDownList1"
InitialValue="Select"
ErrorMessage="Select*"
ForeColor="Red" >
</asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td style="width: 180px; height: 22px;"></td>
<td style="height: 22px"></td>
<td style="height: 22px"></td>
</tr>
<tr>
<td style="width: 180px"> </td>
<td> </td>
<td> </td>
</tr>
</table>
</asp:Panel>
</asp:Content>
.aspx.cs code: (code behind,including only required)
protected void gvCourses_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "delete")
{
SqlConnection con =
new SqlConnection(ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString);
DataTable dt = new DataTable();
string query =
"delete from tblCourses where course='"+e.CommandArgument.ToString()+"'";
SqlCommand cmd = new SqlCommand(query, con);
con.Open();
int n = cmd.ExecuteNonQuery();
con.Close();
gvCourses.DataSource = dt;
gvCourses.DataBind();
ViewState["query"] = "select course from tblCourses";
bindgrid();
}
protected void bindgrid()
{
SqlConnection con =
new SqlConnection(ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString);
DataTable dt = new DataTable();
using (SqlDataAdapter sda = new SqlDataAdapter(ViewState["query"].ToString(), con))
{
sda.Fill(dt);
}
gvCourses.DataSource = dt;
gvCourses.DataBind();
}
Page looks like this(batches haven't been coded yet).
There seems to be some built-in gridview code that is created and fires when a command button is called Delete. According to this, this and this you can just change the name from Delete to anything else. Otherwise, add the empty event handler.
Btw, DataSets have CRUD functionality built-in and they can be easier to work with, but some people don't like the overhead.
Here is my ASP repeater code
<asp:repeater id="filterTable" runat="server">
<HeaderTemplate>
<br/><br/><br/>
<div class="table-responsive">
<table width="100%" cellspacing="0" id="table_odd" class="quickordertable table table-bordered table-striped table-condensed grid-table">
<tr>
<th style="width: 80px">
Details
</th>
<th style="width: 80px">
Filter
</th>
<th style="width: 30px">
Qty
</th>
<th style="width: 60px">
Include In Order
</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td><%#Eval("Details")%></td>
<td><%#Eval("FilterType")%></td>
<td><%#Eval("Qty")%></td>
<td><asp:CheckBox ID="orderPartNumber" Checked="true" runat="server"/><asp:HiddenField ID="hiddenPartNumber" Value ='<%#Eval("PartNumber")%>'></td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</div>
</FooterTemplate>
</asp:Repeater>
I have an ASP ItemTemplate with a field of <%#Eval("GenuineOnly")%>
If field GenuineOnly contains a value, (Or is not Null) Then I want to show this code
<td>GENUINE ONLY</td>
Else I want to show this code
<td><asp:CheckBox ID="orderPartNumber" runat="server"/><asp:HiddenField ID="hiddenPartNumber" Value ='<%#Eval("PartNumber")%>'></td>
I've tried a number of different ways but can't seem to get the syntax right! Any help would be appreciated.
You can achieve this using this way:
<td>
<asp:Panel runat="server" Visible='<%#Eval("GenuineOnly").ToString().Length > 0 %>'>
<asp:CheckBox ID="orderPartNumber" Checked="true" runat="server" />
<asp:HiddenField runat="server" ID="hiddenPartNumber" Value='<%#Eval("PartNumber")%>' />
</asp:Panel>
<asp:Panel runat="server" Visible='<%#Eval("GenuineOnly").ToString().Length == 0 %>'>
GENUINE ONLY
</asp:Panel>
</td>
I assume you can add Table/Td as per your need.
<asp:Repeater ID="rptr" runat="server" OnItemDataBound="rptr_ItemDataBound">
<ItemTemplate>
<asp:Label ID="lblGenuineOnly" runat="server" Text='<%#Eval("GenuineOnly") %>' />
<asp:CheckBox ID="orderPartNumber" runat="server"/>
<asp:HiddenField ID="hiddenPartNumber" Value ='<%#Eval("PartNumber")%>' runat="server" />
</ItemTemplate>
Code behind
protected void rptr_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
Label lblPartNumber = (Label)e.Item.FindControl("lblGenuineOnly");
CheckBox chkPart = (CheckBox)e.Item.FindControl("orderPartNumber");
if (string.IsNullOrEmpty(lblPartNumber.Text.Trim())) {
//Display GENUINE ONLY in any label like lblPartNumber.Text = "GENUINE ONLY"
chkPart.Visible = false;
}
else
{
chkPart.Visible = true;
}
}
I'm a beginner with ASP.net and now, I want to limit the item in my asp:repeater (It's a RSS feed)
This is my code :
<asp:Repeater ID="rssRepeater" runat="server">
<ItemTemplate>
<table style="border: solid 1px black; width: 500px; font-family: Arial">
<tr>
<td style="font-weight: bold">
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%#Eval("link")%>' Text='<%#Eval("title")%>'></asp:HyperLink>
</td>
</tr>
<tr>
<td>
<hr />
</td>
</tr>
<tr>
<td style="background-color: #C2D69B">
<asp:Label ID="Label1" runat="server" Text='<%#Eval("description")%>'></asp:Label>
</td>
</tr>
</table>
<br />
</ItemTemplate>
</asp:Repeater>
EDIT :
Code behind
try
{
WebResponse rep = rssReq.GetResponse();
XmlTextReader xtr = new XmlTextReader(rep.GetResponseStream());
DataSet ds = new DataSet();
ds.ReadXml(xtr);
rssRepeater.DataSource = ds.Tables[2];
rssRepeater.DataBind();
}
catch (Exception ex)
{
throw ex;
}
have you got a property to limit the display of items ?
Thank you for your help !
There is no properties in the repeater that controls the number of rows but you can control it from the DataSource or the SQL query that binds this repeater
I have a repeater trying to get multiple data to display.
which includes a few textboxes which will show the current setting.
take note that this acts like a 'edit info' page for multiple images at once.
i also have problem displaying the images from the database.
To make it simple :
my .cs code:
DataTable ChildImageDT = myImagesBAL.GetChildImageDT(userID, childID, display);
var userList = new List<Images>();
foreach (DataRow row in ChildImageDT.Rows)
{
var child = new Images()
{
DateTaken = DateTime.Parse(row["image_taken_dt"].ToString()),
PlaceTaken = row["image_taken_loc"].ToString(),
DetailedInfo = row["image_info"].ToString()
};
userList.Add(child);
}
Repeater1.DataSource = userList;
Repeater1.DataBind();
my .aspx code
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<table class="content_background">
<tr>
<td width= "10%">Date Taken:</td>
<td><asp:TextBox ID="txtName" Text="<%#Eval("DateTaken")%>" Visible="true" runat="server" Height="100px" Width="100px"></asp:TextBox></td>
</tr>
<tr>
<td width= "10%" bgcolor=aqua>Place Taken:</td>
<td bgcolor=blue ><asp:TextBox ID="txtPassword" Text="<%#Eval("PlaceTaken")%>" Visible=true runat="server" BackColor="White" Font-Size="Large" ForeColor="Fuchsia" Height=50px ></asp:TextBox></td>
</tr>
<tr>
<td width= "10%">Detailed Info:</td>
<td><asp:TextBox ID="TextBox1" Text="<%#Eval("DetailedInfo")%>" Visible=true runat="server" ></asp:TextBox></td>
</tr>
</table>
</ItemTemplate>
my output as shown:
note: that the output is in the "text: " but the whole text box doesnt appear.
You might be getting a "The server tag is not well formed." error.
Just change your Eval code to single quotes instead of a double quote e.g.
Text="<%# Eval("DateTaken") %>" // It's understood as string text
to
Text='<%# Eval("DateTaken") %>' // now understood as server side code.