When I have a table in database with many rows
and I want to display them by normal line (Not with Gridview). Like:
You teach these classes:
class 1/1, class 2/1, class 3/1
we take (1/1 and 2/1 and 3/1) from database.
how can I do it ?
note: I use LINQ to deal with database.
Using a repeater seems to be the easiest way to me. It would look something like this.
<div>
You teach these classes:
<asp:Repeater
ID="rptListOfClasses"
runat="server"
DataSourceID="linqDataSource" >
<ItemTemplate>
<span> class <%# Eval("ClassDate") %>, </span>
</ItemTemplate>
</asp:Repeater>
</div>
<asp:ListView ID="lvClass" runat="server" DataSourceID="LinqDataSource1" OnItemDataBound ="lvResult_ItemDataBound" >
<LayoutTemplate>
You teach these classes:
<asp:PlaceHolder runat="server" ID="itemPlaceHolder" ></asp:PlaceHolder>
</LayoutTemplate>
<ItemTemplate>
<asp:Label ID="lblClass" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Class") + ", " %>' ></asp:Label>
</ItemTemplate>
</asp:ListView>
<asp:LinqDataSource ID="LinqDataSource1" runat="server"
ContextTypeName="MyDataContext" TableName="ClassList"
onselected="LinqDataSource1_Selected">
</asp:LinqDataSource>
Code Behind:
//remove the last comma(,)
public class Test : System.Web.UI.Page
{
int iCount = 0;
protected void lvResult_ItemDataBound(object sender, ListViewItemEventArgs e)
{
if (e.Item.ItemType == ListViewItemType.DataItem)
{
Label lblClass = e.Item.FindControl("lblClass") as Label;
ListViewDataItem lvItem = e.Item as ListViewDataItem ;
if (lblClass != null)
if (lvItem.DataItemIndex == (iCount - 1))
lblClass.Text = lblClass.Text.Substring(0, lblClass.Text.ToString().Length - 2);
}
}
protected void LinqDataSource1_Selected(object sender, LinqDataSourceStatusEventArgs e)
{
iCount = e.TotalRowCount;
}
}
Related
<asp:repeater id="rpt" run="server">
<ItemTemplate>
<asp:LinkButton id="Delete" runat="server" OnCommand="Delete_Command"></asp:linkButton>
<asp:label id="lblMessage" run="server">
</ItemTemplate>
</asp:repeater>
Code Behind:
protected void Delete_Command(object sender, CommandEventArgument e)
{
}
how i get the reference to the "lblMessage" in Delete_Command.
Try this:
protected void Delete_Command(object sender, CommandEventArgs e)
{
LinkButton button = (LinkButton)sender;
Label label = (Label)button.NamingContainer.FindControl("lblMessage");
// do something with the label
}
If you:
Have bound the repeater
Have ViewState enabled
Do not re-bind the repeater earlier in the post back
this should work. If not, please verify that the id of the label is indeed exactly the same as in the ...FindControl("lblMessage");. Also make sure that runat="server" is set on all the controls involved.
Edit: One more thing to check: Search the markup file (the .aspx file) and check if there are any other controls that also use the same event in the code behind. If another control is using the same event handler and that control is not in the repeater, the label will not be found.
means are you want find a lable in Delete_Command event?
in aspx
<asp:Repeater ID="rpt" runat="server">
<ItemTemplate>
<asp:LinkButton ID="Delete" runat="server" OnCommand="Delete_Command"></asp:LinkButton>
<asp:Label ID="lblMessage" run="server">
</ItemTemplate>
</asp:Repeater>
in aspx.cs
protected void Delete_Command(object sender, CommandEventArgs e)
{
foreach (RepeaterItem item in rpt.Items)
{
Label lblMessage = item.FindControl("lblMessage") as Label;
if (lblMessage != null)
{
lblMessage.Text = "";
}
}
}
If You want to make it in your way use following code in
protected void Repeater1_ItemCommand(object source, CommandEventArgs e)
{
(((LinkButton)source).NamingContainer).FindControl("lblName")
}
Another approach.. But something that you can buy
aspx
<asp:Repeater ID="Repeater1" runat="server"
onitemcommand="Repeater1_ItemCommand">
<ItemTemplate>
<asp:Label ID="lblName" runat="server" Text='<%=Eval("Name") %>' ></asp:Label>
<asp:LinkButton runat="server" CommandName="Delete_Command" Text="sd"></asp:LinkButton>
</ItemTemplate>
</asp:Repeater>
.cs
protected void Delete_Command(object sender, CommandEventArgument e)
{
if(e.CommandName != null)// Conditional Check
{
Label label = e.Item.FindControl("lblMessage");
// do something with the label
}
}
In asp.net application, I am using grid-view control in that I am binding the data to the label which is in grid-view.
If data is empty then the color of the row should be in red
If not I mean if data is there to bind then the row in green.
This is my code:
<asp:TemplateField HeaderText ="Holiday Region">
<ItemTemplate >
<asp:Label ID ="lblholdareg" runat ="server" Text ='<%# Eval("Holidaregion") %>' >
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
You need to handle the RowDataBound event, get into the e.Row item, and assign either a CSS class or directly set the background color. I prefer setting a CSS class so you can change the rendering of it without a recompile later.
<asp:GridView ID="GridView1" runat="server" OnRowDataBound="GridView1_RowDataBound">
<Columns>
<asp:TemplateField HeaderText="Holiday Region">
<ItemTemplate>
<asp:Label ID="lblholdareg" runat="server" Text='<%# Eval("Holidaregion") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
And the code-behind, I had to assume you were using a DataTable as your data source, update the code to fit your data structure:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
System.Data.DataRow row = (System.Data.DataRow)e.Row.DataItem;
if (row["Holidaregion"] == null || row["Holidaregion"].ToString().Trim().Length == 0)
{
e.Row.CssClass = "row-empty";
}
else
{
e.Row.CssClass = "row-full";
}
}
You can do it on rowdatabound function of gridview as follows
protected void RowDataBound(Object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
//change it according your cell number or find element
if(e.Row.Cells[0].Text != "")
e.Row.BackColor = Color.Green;
else
e.Row.BackColor = Color.Red;
}
}
if(e.Row.RowType == DataControlRowType.DataRow)
{
Control l = e.Row.FindControl("Label1");
((Label)l).BackColor = System.Drawing.Color.Red;
}
Try something like this
<asp:TemplateField HeaderText ="Holiday Region">
<ItemTemplate >
<asp:Label ID ="lblholdareg" runat ="server"
CSSClass='<%# (String.IsNullOrEmply(Eval("Holidaregion")))?"red:green" %>'
Text ='<%# Eval("Holidaregion") %>' >
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
Edit:
Instead of fighting with grid view inline and code behind stuffs, just use a jQuery and achieve the same in client side
try it this code it is every row in color change with the category wise or filters wise
http://devloper4u.blogspot.in/2013/10/how-to-set-color-in-every-row-on.html
I have a gridview like this:
<asp:GridView ID="gv1" AutoGenerateColumns="false" BorderWidth="0" runat="server" >
<Columns>
<asp:TemplateField>
<ItemTemplate>
<span style="font-family:Tahoma; font-size:14px;">
<u> <a href="<%#DataBinder.Eval(Container.DataItem,"ShUrl")%>">
<%#DataBinder.Eval(Container.DataItem,"PostTitle")%>
</a>
</u>
<br />
</span>
<asp:Repeater ID="rp1" runat="server">
<HeaderTemplate>
<ul>
</HeaderTemplate>
<ItemTemplate>
<li >
<%# Eval("TagName")%>
</li>
</ItemTemplate>
<FooterTemplate>
</ul>
</FooterTemplate>
</asp:Repeater>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Now I am able to load successfull ShUrl and PostTitle. I am bringing titleId from the database also.
Now as usual, a post might have several tag. So I want to load the repeater for the particular titleId.
On server side, I am simply binding the gv1 from the datable.
Now how to load tags for the titleid:
I have already written the function on the server side, might help you all to guide me:
private void LoadtagList(int titleId)
{
// calling DAL
rp1.DataSource = db.GetAllTagsForPost(titleId);
rp1.DataBind();
}
you have to use GridView RowDataBound Event for that
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
System.Data.DataRowView dr = (System.Data.DataRowView)e.Row.DataItem;
if (Convert.ToString(dr["titleId"]) != "")
{
Repeater rp1 = (Repeater)e.Row.Findcontrol("rp1");
rp1.DataSource = db.GetAllTagsForPost(titleId);
rp1.DataBind();
}
}
}
I have a repeater control and textbox in that repeater. I want a required field validator in the textbox ho can i do this
<asp:Repeater id="myRep" OnItemDataBound="rep_ItemDataBound" runat="server">
<ItemTemplate>
<asp:TextBox id="tx" runat="server" />
<asp:RequiredFieldValidator id="myVal" ControlToValidate="tx" ErrorMessage="Required" runat="server" />
</ItemTemplate>
</asp:Repeater>
UPDATE
Add this to code-behind (also modify the markup a bit to subscribe to an event, see above):
protected void rep_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
RequiredFieldValidator val = (RequiredFieldValidator)e.Item.FindControl("myVal");
TextBox tb = (TextBox)e.Item.FindControl("tx");
val.ControlToValidate = tb.ID;
}
If you want all textboxes in a repeater to be validated by a single button click then thats simple like this
<asp:Repeater ID="rpt" runat="server">
<ItemTemplate>
<asp:TextBox ID="txt" runat="server">
</asp:TextBox>
<asp:RequiredFieldValidator ID="rqf" ControlToValidate="txt"
ErrorMessage="*" ValidationGroup = "TestValidationGroup" runat = "server">
</asp:RequiredFieldValidator>
</ItemTemplate>
</asp:Repeater>
<asp:Button ID = "btnSubmit" runat = "server" ValidationGroup = "TestValidationGroup" />
No need to check for any event for databound or anything.
You can set ControlToValidate value on repeater itemdatabound.
Try this one
<asp:Repeater ID="rptSample" runat="server">
<ItemTemplate>
Name:<br />
<asp:TextBox ID="txtName" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvName" ControlToValidate="txtName" runat="server" Display="Static" ErrorMessage="Name field cannot be left blank"></asp:RequiredFieldValidator>
</ItemTemplate>
</asp:Repeater>
<br />
<asp:Button ID="btnSubmit" Text="Submit" runat="server" />
protected void myRep_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
((RequiredFieldValidator)e.Item.FindControl("myVal")).ValidationGroup = ((TextBox)e.Item.FindControl("tx")).UniqueID;
}
}
protected void Repeater_OnItemDataBound(object sender, RepeaterItemEventArgs e) {
tblData tbldata = e.Item.DataItem as tblData;
RequiredFieldValidator val = (RequiredFieldValidator)e.Item.FindControl("rfv");
if (tbldata.FieldName.ToUpper().Contains("NOT NULL")) {
TextBox tb = (TextBox)e.Item.FindControl("txtDATFieldName");
val.ControlToValidate = tb.ID;
}
else {
val.Enabled = false; // disable when you dont need a validator
}
}
Maybe you want have a validation per row... Set the validation group to a group per row like this
ValidationGroup='<%# "gropname" + Eval("Id") %>'
do this in the validator, textbox and the button
HTH
G.
I kept getting a duplicate key exception in RegisterExpandoAttribute trying to do this.
I was using a UserControl inside a repeater, when "OnDataBinding" instead of "ItemDataBinding"
This worked for me:
/// <summary>
/// Raises the <see cref="E:System.Web.UI.Control.DataBinding" /> event.
/// </summary>
/// <param name="e">An <see cref="T:System.EventArgs" /> object that contains the event data.</param>
protected override void OnDataBinding(EventArgs e)
{
base.OnDataBinding(e);
foreach (Control ct in this.Controls)
{
BaseValidator bv = ct as BaseValidator;
if (null == bv)
{
continue;
}
bv.ControlToValidate = this.FindControl(bv.ControlToValidate).ID;
bv.ValidationGroup = this.ValidationGroup;
}
}
And yes, I don't think it makes any sense either
here is my problem:
I've got a repeater on my asp.net (VB):
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("Question_Number") %>' />
<%#Eval("Question_Desc")%>
Now what I want to do is, check a value that I haven't used called "Question_Type" which could be = 1, 2 or 3 depending if it is multiple choice, short answer, etc.
I have tried this:
<%
if Eval("Question_type") = 1 then
Response.Write(" <asp:RadioButton runat=""server"">test1</asp:RadioButton>")
Response.Write(" <asp:RadioButton runat=""server"">test2</asp:RadioButton>")
Response.Write(" <asp:RadioButton runat=""server"">test3</asp:RadioButton>")
end if
%>
and I get this error:
Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.
HOW can I use this value in a if statement???
You are going to need to handle the ItemDataBound event and manually handle the values there.
Here is how I might approach the problem given this repeater:
<asp:Repeater ID="Repeater1" runat="server" OnItemDataBound="HandleQuestionType">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("Question_Number") %>' />
<%#Eval("Question_Desc")%>
<asp:PlaceHolder ID="phQuestions" runat="server" />
</ItemTemplate>
</asp:Repeater>
Here is my event handler for getting the possible answers to a radio button list:
protected void HandleQuestionType(object sender, RepeaterItemEventArgs e)
{
// Execute the following logic for Items and Alternating Items.
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
var question = e.Item.DataItem as Question;
var placeHolder = e.Item.FindControl("phQuestions") as PlaceHolder;
if(question != null && placeHolder != null)
{
if(question.Question_Type == QuestionTypeEnum.MultipleChoice)
{
var radioList = new RadioButtonList
{
DataTextField = "Answer",
DataValueField = "ID",
DataSource = GetPossibleAnswers()
};
radioList.DataBind();
placeHolder.Controls.Add(radioList);
}
}
}
}