Access Repeater controls propeties - c#

I need to to use Response.Redirect() to redirect to a page with a query string value, because i can't set this value.
I have a repeater that contains ImageButton and a Label, the label's text is retrieved from DB, i need to response to a page with URL"ShowCourse.aspx?coursename=value" where value is the value of the label.
Code of repeater:
<asp:Repeater ID="CoursesListRepeater" runat="server" DataSourceID="FetchCourse">
<ItemTemplate>
<table>
<tr>
<td>
<asp:ImageButton ID="CourseImage" runat="server" Height="90" Width="90" ImageUrl='<%# Eval("CourseImage")%>' CssClass="imageButtons" OnClick="CourseImage_Click" />
</td>
<td>
<asp:Label runat="server" ID="CourseNameLabel"><%#Eval("CourseName") %></asp:Label>
<br />
<asp:Label ID="CategoryLabel" runat="server" Font-Size="22px" ForeColor="Gray" Text='<%#Eval("CourseCategory") %>' />
<br />
<asp:Label ID="DescriptionLabel" Font-Size="13px" runat="server" Text='<%#Eval("CourseDescription") %>' ForeColor="DarkRed" />
</td>
</tr>
<hr style="margin-top: 30px; border-radius: 5px;" />
</table>
</ItemTemplate>
</asp:Repeater>

You can use CommandName & CommandArgument on the ImageButton like this:
Template:
<asp:Repeater ID="CoursesListRepeater" runat="server" DataSourceID="FetchCourse"
onitemcommand="CoursesListRepeater_ItemCommand"></asp:Repeater>
....
<asp:ImageButton ID="CourseImage" runat="server" Height="90" Width="90" ImageUrl='<%# Eval("CourseImage")%>' CssClass="imageButtons"
CommandName="ShowCourse" CommandArgument='AboutCourse.aspx?coursename=<%# Eval("CourseName") %>' />
Code behind:
protected void CoursesListRepeater_ItemCommand(object source, RepeaterCommandEventArgs e)
{
if (e.CommandName == "ShowCourse")
{
Response.Redirect(e.CommandArgument.ToString());
}
}

Better to add a property named Url to your class and use the above method and set the CommandArgument='<%# Eval("Url")%>'
Keep in mind that you have to create the redirect url from our code behind and set it to Repeater's data items's Url property you newly added

Related

Get textbox value in repeater control when button in same row is clicked

I have this repeater:
<asp:Repeater ID="rptImages" runat="server">
<HeaderTemplate>
<table>
<tr>
<th>Image</th>
<th>Caption</th>
<th> </th>
<th>Update File</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<asp:Image Width="108px" Height="67px" runat="server" ID="imgDb" ImageUrl='<%# Eval("imageUrl") %>' />
</td>
<td>
<asp:TextBox runat="server" style="margin-left: 5px; margin-right: 5px;" ID="txtCaption" Text='<%# Eval("caption") %>'></asp:TextBox>
</td>
<td>
<td><asp:FileUpload ID="fu" runat="server" /></td>
<td><asp:Button ID="btn" runat="server" Text="Update" CssClass="btn btn-info" CommandArgument='<%# Eval("id") %>' OnClick="btn_OnClick" /></td>
<td><asp:Button runat="server" ID="btnDelete" CssClass="btn btn-danger" CommandArgument='<%# Eval("id") %>' OnClick="btnDelete_OnClick" Text="Delete" /></td>
<td><asp:HiddenField ID="lblC" runat="server" Value='<%# DataBinder.Eval(Container.DataItem, "id") %>'></asp:HiddenField></td>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
I want to know how can I get the textbox specifically from the row where the update button is clicked. What logic should I use to get the value I need?
You could probably do that with handling Click event on the button and then getting its parent control. But I would recommend handling command of the repeater instead:
<asp:Repeater ID="rptImages" runat="server"
OnItemCommand="rptImages_ItemCommand">
Remove the handler from the button, but keep the command arg. You may also want to set command name if there are other controls sending commands from within the repeater:
<asp:Button runat="server" ID="btnDelete" CssClass="btn btn-danger"
CommandArgument='<%# Eval("id") %>'
Text="Delete" />
And in the code behind this becomes trivial:
protected void rptImages_ItemCommand(object sender, RepeaterCommandEventArgs e)
{
TextBox t = e.Item.FindControl("txtCaption") as TextBox;
}

linkbutton or button onClick not fireing inside gridview ItemTemplate

hi i have a gridview in my asp.net webpage that is bounded after a user selects 3 dropdown list.
dropdown list's auto postbacks are set true , now my problem is when i put a button , imagebutton or linkbutton in my gridview ItemTemplate the onclick event dosent fire !
here is my code
<asp:GridView ID="GridView1" runat="server" Width="90%" Height="100%" OnRowCommand="GridView1_RowCommand" OnPageIndexChanging="GridView1_PageIndexChanging" style="margin:20px; vertical-align:top;" PageSize="10" AllowPaging="True"
AllowSorting="True"
AutoGenerateColumns="False" DataKeyNames="WFStudentID" ShowHeader="true" BorderWidth="1px">
<Columns>
<asp:TemplateField ShowHeader="true">
<ItemTemplate >
<tr>
<td>
<asp:Label ID="Label1" ForeColor="Silver" Font-Size="Medium" runat="server" Text='<%# Bind("Name") %>'></asp:Label>
</td>
<td>
<asp:Label ID="Label2" runat="server" ForeColor="Silver" Font-Size="Medium" Text='<%# Bind("Family") %>'></asp:Label>
</td>
<td>
<asp:HyperLink ID="HyperLink1" ForeColor="Silver" Font-Size="Medium" Target="_blank" NavigateUrl='<%# Bind("WFStudentFilePath") %>' runat="server">دانلود</asp:HyperLink>
</td>
<td>
<asp:Label ID="Label7" runat="server" ForeColor="Silver" Font-Size="Medium" Text='<%# Bind("WFStudentDate") %>'></asp:Label>
</td>
<td>
<asp:Button ID="Deny" Text="deny" OnClick="Deny_Click1" runat="server" />
</td>
<td>
<asp:ImageButton ID="accept" OnClick="accept_Click" runat="server" />
</td>
<td>
<asp:TextBox ID="des" TextMode="MultiLine" Height="100px" Width="200px" runat="server"></asp:TextBox>
</td>
</tr>
<br />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
protected void Deny_Click1(object sender, EventArgs e)
{
Response.Redirect("home.aspx");
}
It is probably firing, but it will throw an exception (which your catch block ignores). The exception is because of this line:
LinkButton Link = (LinkButton)sender;
The sender is a Button, not a LinkButton, so the cast is not valid and an exception will be thrown.
you can check this::
http://www.dotnetbull.com/2013/05/how-to-handle-click-event-of-linkbutton.html
verify your code like this
Buttons inside GridView do not respond to direct events. They have to go through GridView's RowCommand event implementation. I've seen that you have implemented OnRowCommand="GridView1_RowCommand". So now you need make following change to image button:
<asp:ImageButton ID="accept" runat="server" CommandName="Accept" />
Now look for "Accept" command name in your GridView1_RowCommand event handler.
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Accept")
{
// Your code goes here
}
}
I hope this helps.

C#,asp.net gridview ,edit a value using the radiobutton

I am using a grid in which inside the item template(the first one in which radio button is there). i use link button instead of radiobutton ,when i click on it iam able to edit as the textbox appears, but if i use the radio button,that editing option is not coming;
aspx:
<div id="loginblock">
<table>
<tr>
<td>UserName:</td>
<td>
<asp:TextBox ID="username" runat="server" onBlur="txtvalidation();" AutoPostBack="true"></asp:TextBox>
</td>
<td>
<div id="warning" style="color:aqua;width:100px" runat="server">
</div>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="PlS Enter Password" ControlToValidate="pwd"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td>Password:</td>
<td>
<asp:TextBox ID="pwd" runat="server" TextMode="Password" ></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Button ID="btnLogin" runat="server" OnClick="btnLogin_Click" Text="Button" />
</td>
</tr>
<tr>
<asp:GridView ID="grd" runat="server" OnRowEditing="selectrd_CheckedChanged" AutoGenerateColumns="false" DataKeyNames="userid">
<Columns>
<asp:TemplateField HeaderText="edition">
<ItemTemplate>
<asp:RadioButton ID="rdnselect" runat="server" CommndName="edit" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField HeaderText="ids" DataField="userid" />
<asp:TemplateField HeaderText="password">
<ItemTemplate>
<%# Eval("pwd") %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="edits" runat="server" Text='<%# Eval("pwd") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
cs:
protected void selectrd_CheckedChanged(object sender, GridViewEditEventArgs e)
{
grd.SelectedIndex = e.NewEditIndex;
ldgrid();
}
Here if we remove the radiobutton and insert link button,onclick of the link button we can edit the values,pls say me how to do the same operation with the radio button.

Changing Readonly attribute of Textbox within a repeater

On my aspx page i have a repeater with 5 textboxes with 1 imagebutton to edit the row
these textboxes are readonly, to edit them I need them to not be readOnly..
In my behind code i am using :
protected void EditRecipeInfo(object sender, CommandEventArgs e)
{
ImageButton ib = sender as ImageButton;
TextBox titleTXT = (TextBox)ib.FindControl("titleRepeat");
TextBox qtyTXT = (TextBox)ib.FindControl("qtyRepeat");
TextBox uomTXT = (TextBox)ib.FindControl("uomRepeat");
TextBox prepTXT = (TextBox)ib.FindControl("prepRepeat");
TextBox orTXT = (TextBox)ib.FindControl("orRepeat");
titleTXT.ReadOnly = false;
qtyTXT.ReadOnly = false;
uomTXT.ReadOnly = false;
prepTXT.ReadOnly = false;
orTXT.ReadOnly = false;
////
}
But when I fire this event the break points show me that the property is being set to false, but when I click to delete any value in the textbox it still acts like a readonly
UPDATE:
<asp:Repeater ID="ingredRepeater" runat="server">
<HeaderTemplate>
<table style="width: 100%">
<tr>
<th></th>
<th></th>
<th><h2>Title</h2></th>
<th><h2>Qty.</h2></th>
<th><h2>UoM</h2></th>
<th><h2>Prep.</h2></th>
<th><h2>Alternate</h2></th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<asp:ImageButton Style="height: 25px; width: 25px;" ImageUrl="/img/edit.png" Visible="true"
ID="editRecipeInfo" AutoPostBack="true" runat="server" OnCommand="EditRecipeInfo" CommandName='<%# DataBinder.Eval(Container, "DataItem.DetailID") %>' />
</td>
<td>
<asp:ImageButton ImageUrl="/img/RedX.png" ID="button2" runat="server" Height="20"
Width="20" CommandName='<%# DataBinder.Eval(Container, "DataItem.DetailID") %>'
OnCommand="deleteRecipeView" />
</td>
<td>
<asp:TextBox AutoPostBack="true" ReadOnly="true" ID="titleRepeat" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.Name") %>'
size="45" />
</td>
<td>
<asp:TextBox AutoPostBack="true" ReadOnly='true' ID="qtyRepeat" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.Quantity") %>'
size="10" />
</td>
<td>
<asp:TextBox AutoPostBack="true" ReadOnly='true' ID="uomRepeat" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.UnitsOfMeasure") %>'
size="10" />
</td>
<td>
<asp:TextBox AutoPostBack="true" ReadOnly='true' ID="prepRepeat" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.Prep") %>'
size="10" />
</td>
<td>
<asp:TextBox AutoPostBack="true" ReadOnly='true' ID="orRepeat" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.AlternativeIngredients") %>'
size="20" />
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
Make sure you're not re-binding the repeater after you set the ReadOnly property to true.
I agree with #Tariqulazam , the markup would help.
Assuming your code comes from an ItemCommand event handler, I am quite surprised to see the FindControl applied to the ImageButton.
I guess your code should be something like this :
void rpAcces_ItemCommand(object source, RepeaterCommandEventArgs e)
{
//...
ImageButton ib = sender as ImageButton;
TextBox titleTXT = (TextBox)e.Item.FindControl("titleRepeat");
TextBox qtyTXT = (TextBox)e.Item.FindControl("qtyRepeat");
TextBox uomTXT = (TextBox)e.Item.FindControl("uomRepeat");
TextBox prepTXT = (TextBox)e.Item.FindControl("prepRepeat");
TextBox orTXT = (TextBox)e.Item.FindControl("orRepeat");
titleTXT.ReadOnly = false;
qtyTXT.ReadOnly = false;
uomTXT.ReadOnly = false;
prepTXT.ReadOnly = false;
orTXT.ReadOnly = false;
//...
}
Also, be aware that you can not rebind your repeater later in the page life-cycle without losing these changes.
And watch out for any Enabled attribute set on your TextBoxes
Once again, difficult to answer without the whole code.

Panel visibilty on/off toggle effect not working

I've a got ListBox called lbxUpcommingEvents. When the index is changed the event handler is fired to check for duplicate records. If duplicates are not found, a panel called pnlAction inside a formview is turned on by the way of display style. If dups are found another panel pnlActionCancel is turned on and the oter is tuned off. Basically a toogle effect.
I've tried the visible property, viewstate property, but it does not work and I can't figure it out so once again, I seek wizdom from the collective. Here is my code.
protected void lbxUpcommingEvents_OnSelectedIndexChanged(object sender, EventArgs e)
{
pnlEventsSignUp.Visible = true;
string _selectedItemValue = lbxUpcommingEvents.SelectedValue.ToString();
int _eventid = Convert.ToInt32(_selectedItemValue);
Guid _memberId = Guid.Empty;
_memberId = new Guid(Session["myId"].ToString());
// Check for existing signup
EventsMemberSignup _createSingup = new EventsMemberSignup();
dsEventsSingupTableAdapters.MemberEventsTableAdapter da = new dsEventsSingupTableAdapters.MemberEventsTableAdapter();
dsEventsSingup.MemberEventsDataTable dt = da.GetDataForDupCheck(_memberId, _eventid);
if (dt.Rows.Count == 1)
{
Panel pnlAction = (Panel)(fvEventSignUp.FindControl("pnlAction"));
//pnlAction.Visible = false;
pnlAction.Style.Add("display","none");
Panel pnlActionCancel = (Panel)(fvEventSignUp.FindControl("pnlActionCancel"));
//pnlActionCancel.Visible = true;
pnlActionCancel.Style.Remove("display");
}
else
{
Panel pnlActionCancel = (Panel)(fvEventSignUp.FindControl("pnlActionCancel"));
//pnlActionCancel.Visible = false;
pnlActionCancel.Style.Add("display", "none");
Panel pnlAction = (Panel)(fvEventSignUp.FindControl("pnlAction"));
//pnlAction.Visible = true;
pnlAction.Style.Remove("display");
}
}
<div id="columnleft">
<a name="content_start" id="content_start"></a>
<div class="leftblock">
<h2>Events Signup</h2>
<p>
</p>
<h3> Upcomming Events</h3>
<p>
<asp:ListBox ID="lbxUpcommingEvents" runat="server" DataSourceID="odsUpcommingEvents"
Rows="6" DataTextField="Title" DataValueField="id" AutoPostBack="true"
Width="206px" OnSelectedIndexChanged="lbxUpcommingEvents_OnSelectedIndexChanged" />
</p>
<h3> Members Attending</h3>
<p>
<asp:DataGrid ID="lboxSignedUpMembers" runat="server" DataSourceID="odsSignedUpMembers"
AutoPostBack="true" AutoGenerateColumns="false" RowStyle-CssClass="gridview" AlternatingRowStyle-CssClass="altbgcolor"
Width="206px" onselectedindexchanged="lboxSignedUpMembers_SelectedIndexChanged" CssClass="gridview"
GridLines="None" BorderStyle="Solid" BorderWidth="1" BorderColor="Black" >
<AlternatingItemStyle BackColor="White" />
<Columns>
<asp:BoundColumn DataField="Name" />
<asp:BoundColumn DataField="Title" />
<asp:TemplateColumn >
<ItemTemplate>
<asp:Label runat="server" ID="lblDate" Text='<%# Eval("StartTime", "{0:d}") %>' />
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>
</p>
</div>
</div>
</td>
<td align="left" >
<!--Start of right column-->
<div id="columnright">
<div class="rightblock">
<asp:Panel ID="pnlEventsSignUpTitle" runat="server" CssClass="actionbuttons">
<h2>Select an Event to Signup</h2>
</asp:Panel>
<asp:Label runat="server" ID="lblNote" ForeColor="#cc0000" Font-Bold="true" />
</div>
<div class="rightblock">
<asp:Panel runat="server" ID="pnlEventsSignUp" visible="false">
<div class="dashedline" ></div>
<asp:FormView ID="fvEventSignUp" runat="server" DataSourceID="ObjectDataSource1"
DataKeyNames="id" Width="100%" >
<ItemTemplate>
<h2>
<asp:HiddenField runat="server" ID="hfEventID" Value='<%# Eval("id") %>' />
<asp:Label Text='<%# Eval("title") %>' runat="server" ID="titleLabel" />
</h2>
<div class="itemdetails">
<br />
location:
<h3>
<asp:Label ID="locationLabel" runat="server" Text='<%# ShowLocationLink(Eval("locationname"),Eval("location")) %>' />
</h3>
<p>
<asp:Label Text='<%# Eval("starttime","{0:D}") %>' runat="server" ID="itemdateLabel" CssClass="GeneralText" />
<asp:Label Text='<%# ShowDuration(Eval("starttime"),Eval("endtime")) %>' runat="server" ID="Label1" CssClass="GeneralText" />
</p>
</div>
<div class="downloadevent">
<a href="#">
<img src="images/icon_download_event.gif" alt="Download this event to your personal calendar"
width="15" height="26" /></a><a href='<%# "events_download.ashx?EventID=" + Convert.ToString(Eval("id")) %>'>Add
this event to your personal calendar</a>
</div>
<Club:ImageThumbnail ID="thumb1" runat="server" ImageSize="Large" PhotoID='<%# Eval("photo") %>' />
<p>
<asp:Label Text='<%# Eval("description") %>' runat="server" ID="descriptionLabel" />
</p>
<div class="dashedline" ></div>
<asp:Panel ID="pnlAction" runat="server" CssClass="actionbuttons" >
<table border="0" cellpadding="2" cellspacing="2">
<tr>
<td>
<asp:Label runat="server" ID="Label2" Text="Action<br />Required" Width="80px" Font-Bold="true"
ForeColor="#cc0000" Font-Size="14px" />
</td>
<td>
<img src="images/RedArrow.jpg" alt="Red Arrow Right" />
</td>
<td>
<asp:CheckBox runat="server" ID="cbxCert" Height="30px" Text="Check to Confirm Attendance" /><br />
<asp:CustomValidator runat="server" ID="rfvConfirm"
ErrorMessage="You must check the box to continue" Font-Bold="true"
ForeColor="#cc0000" ClientValidationFunction="ensureChecked" /> <br />
<Club:RolloverLink ID="rlnkSignUp" runat="server" Text = "I will be attending this event"
OnClick="rlnkSignUp_OnClick" ToolTip="I hereby certify that I am commiting to attending this event."
/>
</td>
</tr>
</table>
</asp:Panel>
<asp:Panel runat="server" ID="pnlActionCancel" CssClass="actionbuttons" >
<table border="0" cellpadding="2" cellspacing="2">
<tr>
<td>
<asp:Label runat="server" ID="lblDupSignup" Text="You are alredy signed up for this event" ForeColor="#cc0000" Font-Bold="true" Font-Size="14px" />
</td>
<td>
</td>
<td>
<asp:CheckBox runat="server" ID="cbxCancel" Height="30px" Text="Check to Cancel Attendance" /><br />
<asp:CustomValidator runat="server" ID="CustomValidator1"
ErrorMessage="You must check the box to continue" Font-Bold="true"
ForeColor="#cc0000" ClientValidationFunction="ensureChecked" /> <br />
<Club:RolloverLink ID="rlnCancel" runat="server" Text="I'm cancelling my participation"
OnClick="rlnCancel_OnClick" />
</td>
</tr>
</table>
</asp:Panel>
</ItemTemplate>
</asp:FormView>
</asp:Panel>
</div>
I've set the ViewStateMode to "Disabled". I'm just hoping it won't back fire somewhere else.
Your existing code might work if you drop an update panel and place your controls inside it.
Alternatively you can create a property in your page like
private bool myRowCount;
protected bool HasRowsMyData
{
get { return myRowCount; }
set { myRowCount=value; }
}
Then inside your selectedIndex change event
if (dt.Rows.Count == 1)
{
HasRowsMyData=true;
//DataBind your controls here
YourUpdatePanel.Update();
}
Inside your aspx on panels you can set each panel's visible property like this so whenever page is updated they will turn on/off depeding upon the result
Visible='<%#HasRowsMyData%>'
Visible='<%#!HasRowsMyData%>'
If you like to do it client side I advise jQuery's toggle function
jQuey:toggle()

Categories