‘If’ condition for Repeater control

This is my requirement: I have to show some values and controls in a repeater control depending on certain conditions. For example, I have to show the User Name from the database, one label control with some text, a text box and finally an image button. The text on the label control will change according to the conditions and the visibility of both textbox and button will change according to the condition. I have to write the common if condition; but where? I have tried it writing in the aspx page. I tried it out with various method to implement this. None was successful. However last I got the solution.

The HTML section:

<asp:Repeater ID=”rptSearch” runat=”server” DataSourceID=”SqlDataSource1″ OnItemDataBound=”rptSearch_ItemDataBound”>

<div style=”float:left”><asp:Label ID=”lblName” Text='<%#Eval(“FirstName”)%>’ runat=”server”></asp:Label></div>
<div style=”float:left”><asp:Label ID=”lblFrndComt” Text=’Friend Comment:’ runat=”server”></asp:Label></div>
<div style=”float:left”><asp:TextBox ID=”txtComment” runat=”server” Height=”50px” TextMode=”MultiLine” Width=”90%”></asp:TextBox></div>
<div style=”float:left”><asp:ImageButton ID=”imgSubmit” runat=”server” OnClick=”imgSubmit_Click” ImageUrl=”~/Images/submit_btn.gif” CommandArgument='<%#DataBinder.Eval(Container.DataItem,”UserID”) %>’/> </div>


Now in the Code Behind:

Take the Repeater ItemDataBound Method, write the condition:

protected void rptSearch_ItemDataBound(object sender, RepeaterItemEventArgs e)
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)

//Here starts my condition
if (((DataRowView)e.Item.DataItem).Row[“Accept”].ToString() == “2”)
TextBox txtComment = (TextBox)e.Item.FindControl(“txtComment”);
txtComment.Visible = false;
txtComment.TextMode = TextBoxMode.SingleLine;
ImageButton imgSubmit = (ImageButton)e.Item.FindControl(“imgSubmit”);
imgSubmit.Visible = false;
Label lblComment = (Label)e.Item.FindControl(“lblComment”);
lblComment.Text = “<b>We are awaiting a response:</b>”;

If the value of ‘Accept’ returning from the database is other than 2, then theĀ  repeater will show the ‘User Name’, label with text ‘Friend Comment:’, a text box to enter the comments, then a ‘submit’ Image button to submit the comment entered.

Now if the value is 2, then the repeater should show onyl the ‘User Name’ and the label with text ‘We are awaiting a response:’. Also it will hide the text box and the Button.


Empty Row in GridView

If we want to display some text when a GridView control bound to a data source that does not contain any records, we can use a property called EmptyDataText.

<asp:GridView ID=”GridView1″ runat=”server” DataSourceID=”ObjectDataSource1″ AutoGenerateColumns=”False” DataKeyNames=”Id” EmptyDataText=”No Records Found!!”>

Alternatively we can use EmptyDataTemplate.

<EmptyDataTemplate>No Records Found!!</EmptyDataTemplate>

If we used both EmptyDataText and EmptyDataTemplate at the same time, then EmptyDataTemplate property takes precedence.

We can set the appearence of the Empty data text using the property EmptyDataRowStyle.

<EmptyDataRowStyle Font-Bold=”True” HorizontalAlign=”Center” Font-Size=”Larger” />

Moving Delete to the right most column in GridView

In the Gridview control, some times we set the delete option AutoGenerateDeleteButton=”true” for the delete functionality. However the Delete link will appear on the left most column. If we want to make it on the right most column, or somewhere else other than the left most column, what we have to do?

This can be achieved very easily by removing the property AutoGenerateDeleteButton=”true” from the asp:GridView tag then add the column <asp:CommandField ShowDeleteButton=”True” /> where ever we needed, inside the <Column> tag.

This is same in the case of Edit Button, Cancel Button etc.

<asp:GridView ID=”GridView1″ runat=”server” DataSourceID=”ObjectDataSource1″ AutoGenerateColumns=”False” DataKeyNames=”Id”>
<asp:BoundField DataField=”Name” HeaderText=”Name” />
<asp:CommandField ShowDeleteButton=”true” />