I am having problem with the LinkButton onclick event not firing.
I have checked the following posts and taken the precaution of Postback but still joy
repeater linkbutton not firing
Repeater's Item command event is not firing on linkbutton click
Here is my Code so far
<asp:PlaceHolder runat="server" ID="phOrders">
<asp:Repeater ID="rprOrders" runat="server" OnItemCommand="rprOrders_ItemCommand">
<HeaderTemplate>
<table>
<tr>
<th>
<asp:LinkButton ID="lnkOrderByDate" runat="server" Text="Date" CommandName="OrderDate" OnClick="lnkOrderByDate_Click"></asp:LinkButton></th>
<th>
<asp:LinkButton ID="lnkOrderByOrderNumber" runat="server" Text="Order Number"></asp:LinkButton></th>
<th>
<asp:LinkButton ID="lnkOrderByProductNumber" runat="server" Text="Product Number"></asp:LinkButton></th>
<th>Product Description</th>
<th>Size</th>
<th>QTY</th>
<th>Status</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td><strong><%# Eval("OrderDate") %></strong></td>
<td><%# Eval("OrderNumber") %></td>
<td><%# Eval("SKUNumber") %></td>
<td><%# Eval("OrderItemSKUName") %></td>
<td><%# Eval("mtrx_Code2") %></td>
<td><%# Eval("OrderItemUnitCount") %></td>
<td><strong><%# Eval("OrderItemStatus") %></strong></td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
<div class="track-footer"></div>
</asp:PlaceHolder>
Code Behind
protected void SetupControl()
{
if (this.StopProcessing)
{
// Do not process
}
else
{
if (CMSContext.ViewMode == ViewModeEnum.LiveSite)
{
if(!Page.IsPostBack)
{
PopulateProductClass();
PopulateProduct();
PopulateDefaultViewOrders();
}
}
}
}
protected void lnkOrderByDate_Click(object sender, EventArgs e)
{
//Do Something
}
Any suggestions? I can't seem to figure it out?
Even the OnItemCommand="rprOrders_ItemCommand" wont fire either?
The LinkButton within your DataControl triggers the Method rprOrders_ItemCommand
Set a breakpoint there. If you have multiple LinkButton then you can read CommandName="OrderDate" Codebehind: (e.CommandName)
For passing values CommandArgument should be used.
use some thing like this
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" OnCommand="LinkButton1_Command" CommandName="MyUpdate" CommandArgument='<%# Eval("erid") %>'>LinkButton</asp:LinkButton>
</ItemTemplate>
.cs
protected void Repeater1_OnItemCommand(object source, RepeaterCommandEventArgs e)
{
if (e.CommandName.Equals("MyUpdate"))
{
// some code
}
if (e.CommandName.Equals("EditCategory"))
{
// some code
}
}
For me the fix was to set EnableViewState="true" the the control tag of my ascx file.
E.G.
<%# Control Language="C#" AutoEventWireup="True" CodeBehind="Settings.ascx.cs" Inherits="Foo.Bar.Settings" EnableViewState="true" %>
And my LinkButton looks like this
<asp:LinkButton ID="btnRemoveMedia"
runat="server"
class="icon mdi-delete"
OnCommand="btnRemoveMedia_OnCommand"
CommandArgument="<%# ((Tuple<int, string>) Container.DataItem).Item1 %>"
CommandName="Delete"
UseSubmitBehavior="false" />
Related
How to automatic store value in database when checkbox is checked and refresh my To do list. Here is my code:
This is my HTML:
<asp:Repeater ID="rpTodos" runat="server">
<ItemTemplate>
<tr>
<td><%# Eval("TodosID", "{0:d}") %></td>
<td><%# Eval("TaskName", "{0:d}") %></td>
<td><asp:CheckBox ID="cbisdone" runat="server" AutoPostBack="true" checked='<%#Eval("IsDone")%>'></asp:CheckBox></td>
<td><%# Eval("TaskDate", "{0:d}") %></td>
</tr>
</ItemTemplate>
</asp:Repeater>
This is code behind:
var todos2 = ctx.Todos.ToList().Where(t=> t.IsDone == false).ToList();
rpTodos.DataSource = todos2;
rpTodos.DataBind();
Add a OnCheckedChanged event in your aspx page.
<asp:CheckBox ID="cbisdone" runat="server" AutoPostBack="true" checked='<%#Eval("IsDone")%>' OnCheckedChanged="cbisdone_changed"></asp:CheckBox>
Create a new event that would listen to raised event and cast the sender object back to Checkbox.
protected void cbisdone_changed(object sender, EventArgs e)
{
if (sender != null)
{
if (((CheckBox)sender).Checked)
{
// Update the status in DB.
}
}
}
I am trying to get image inside LinkButton in Repeater and wanted to change image Src but it does not changing image UI side.
HTMl Code:
<asp:Repeater ID="Repeater" runat="server" OnItemCommand="Repeater_ItemCommand" >
<HeaderTemplate>
<table c>
<tr>
<th>
<asp:LinkButton ID="lbtnC1" CommandName="Col1" runat="server">Column1 <asp:Image id="imgStamp" ClientIDMode="AutoID" runat="server" style="vertical-align:middle;padding-left:3px" /></asp:LinkButton>
</thalign>
<th>
<asp:LinkButton ID="lbtnC2" runat="server"
CommandName="Col2">Column2 <asp:Image id="imgStamp" ClientIDMode="AutoID" runat="server" style="vertical-align:middle;padding-left:3px" /></asp:LinkButton></th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<asp:Literal ID="C1Value" runat="server" />
</td>
<td>
<asp:Literal ID="C2Value" runat="server" />
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
C# Event
protected void Repeater_ItemCommand(object source, RepeaterCommandEventArgs e)
{
LinkButton linkButton = e.CommandSource as LinkButton;
HtmlImage image = linkButton.Controls[0] as HtmlImage;
if (e.CommandName == "Col1")
{
image.Src = Page.ResolveUrl("~/arrow-down-white.gif");
}
}
Change it to HtmlImage image = linkButton.Controls[1] as HtmlImage;
I believe (but not 100% sure) that the first control is a Literal.
By doing this i'm able to solve my issue.
protected void Repeater_ItemCommand(object source, RepeaterCommandEventArgs e)
{
Image imgStamp = Repeater.Controls[0].Controls[0].FindControl("imgStamp") as Image;
imgStamp.ImageUrl = Page.ResolveUrl("URL");
}
I have a repeater with a HTML Table layout as follows where it fills from the database dynamically,
<asp:Repeater ID="Repeater1" runat="server">
<HeaderTemplate>
<table border="1">
<tr>
<th colspan="2"></th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td><%# DataBinder.Eval(Container.DataItem, "QuestionId") %> <asp:HiddenField ID="hiddenField" Value='<%# DataBinder.Eval(Container.DataItem, "QuestionId") %>' runat="server" /> </td>
<td>
<table>
<tr>
<td colspan="4"><%# DataBinder.Eval(Container.DataItem, "Question") %> </td>
</tr>
<tr><td><asp:RadioButton ID="op1" runat="server" OnCheckedChange="check_Answer"/></td><td><%# DataBinder.Eval(Container.DataItem, "Answer1") %> </td></tr>
<tr><td><asp:RadioButton ID="op2" runat="server" OnCheckedChange="check_Answer"/></td><td><%# DataBinder.Eval(Container.DataItem, "Answer2") %> </td></tr>
<tr><td><asp:RadioButton ID="op3" runat="server" OnCheckedChange="check_Answer"/></td><td><%# DataBinder.Eval(Container.DataItem, "Answer3") %> </td></tr>
<tr><td><asp:RadioButton ID="op4" runat="server" OnCheckedChange="check_Answer"/></td><td><%# DataBinder.Eval(Container.DataItem, "Answer4") %> </td></tr>
</table>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
In Code Behind
protected void check_Answer(object sender, EventArgs e)
{
RepeaterItem item = (sender as RadioButton).Parent as RepeaterItem;
HiddenField hiddenField = item.FindControl("hiddenField") as HiddenField;
String questionId = hiddenField.Value;
Session["test"] = questionId;/*Just added to a session and passed to test page*/
Response.Redirect("test.aspx");/*This Page Displays the value of Session["test"]*/
}
Now what I need to do is OnCheckedChange event for the radio buttons it calls a code behind method, but should pass the QuestionID value to the method. Please suggest me a way to retrieve the value which contains the "QuestionID". For More Information the real interface looks like below,
You can group option buttons and use questionid as a part of group name. In the code you can get the questionid from GroupName. So you don't really need any hidden field. Your markup may look like below:
<asp:Repeater ID="Repeater1" runat="server">
<HeaderTemplate>
<table border="1">
<tr>
<th colspan="2"></th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td><%# DataBinder.Eval(Container.DataItem, "QuestionId") %> </td>
<td>
<table>
<tr>
<td colspan="4"><%# DataBinder.Eval(Container.DataItem, "Question") %> </td>
</tr>
<tr>
<td>
<asp:RadioButton ID="RadioButton1" runat="server" AutoPostBack="true" Text='<%# DataBinder.Eval(Container.DataItem, "Answer1") %>'
OnCheckedChanged="check_Answer" GroupName='<%# Eval("QuestionId","Grp_{0}") %>' />
</td>
<td><asp:RadioButton ID="RadioButton2" runat="server" AutoPostBack="true" Text='<%# DataBinder.Eval(Container.DataItem, "Answer2") %>'
OnCheckedChanged="check_Answer" GroupName='<%# Eval("QuestionId","Grp_{0}") %>' />
</td>
<td><asp:RadioButton ID="RadioButton3" runat="server" AutoPostBack="true" Text='<%# DataBinder.Eval(Container.DataItem, "Answer3") %>'
OnCheckedChanged="check_Answer" GroupName='<%# Eval("QuestionId","Grp_{0}") %>' />
</td>
<td><asp:RadioButton ID="RadioButton4" runat="server" AutoPostBack="true" Text='<%# DataBinder.Eval(Container.DataItem, "Answer4") %>'
OnCheckedChanged="check_Answer" GroupName='<%# Eval("QuestionId","Grp_{0}") %>' />
</td>
</tr>
</table>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
And in the code:
protected void check_Answer(object sender, EventArgs e)
{
string grpId = ((RadioButton)sender).GroupName;
string questionId = grpId.Split('_')[1].ToString();
Session["test"] = questionId;/*Just added to a session and passed to test page*/
Response.Redirect("test.aspx");/*This Page Displays the value of Session["test"]*/
}
I would add hidden field as below
<asp:HiddenField ID="hiddenField" Value='<%# DataBinder.Eval(Container.DataItem, "QuestionId") %>' runat="server" />
in your check_Answer event
RepeaterItem item = (sender as RadioButton).Parent as RepeaterItem;
HiddenField hiddenField = item.FindControl("hiddenField") as HiddenField;
string questionId= hiddenField.Value;
I am trying to input logic in the source view in Asp.Net ListView. The problem is that the program is writing on the screen false or true when executing "If (isItTrue(test))". Does anyone know how to solve this problem?
<%# test= Eval("testId")%>
<%
If (isItTrue(test)) Then
%>
<asp:Button ID="btnTest" runat="server" Text="Like" />
<%
Else
%>
<asp:Label runat="server" Text="hello" </asp:Label>
<%
End If
%>
You could use ItemDataBound to check informations like this and show or hide the controls using your condition. try something like this in your code behine:
protected void ListViewTest_ItemDataBound(object sender, ListViewItemEventArgs e)
{
// if it is data item
if (e.Item.ItemType == ListViewItemType.DataItem)
{
// call your function
if (isItTrue("test"))
{
// show the button
e.Item.FindControl("btnTest").Visible = true;
}
else
{
// show the label
e.Item.FindControl("lblTest").Visible = true;
}
}
}
And in your Listview, you could do something like this, setting the event and adding the controls on the place holder
<asp:ListView ID="ListViewTest" DataSourceID="..." OnItemDataBound="ListViewTest_ItemDataBound" runat="server">
<LayoutTemplate>
<table>
<tr>
<th>Column Name</th>
</tr>
<tr runat="server" id="itemPlaceholder" />
</table>
</LayoutTemplate>
<ItemTemplate>
<tr style="background-color: #CAEEFF" runat="server">
<td>
<%-- both controls are here --%>
<asp:Button ID="btnTest" runat="server" Visible="false" Text="Like"></asp:Button>
<asp:Label ID="lblTest" runat="server" Visible="false" Text="hello"></asp:Label>
</td>
</tr>
</ItemTemplate>
</asp:ListView>
Are you sure it's not this line: <%# test= Eval("testId")%> that is writing true or false to the output?
I've got a repeater with an itemlist, it get's data via C#/sql and a databind.
I then want to add onclick javascript to my 'deleteNewsButton' in the itemlist.
Im guessing that i have to use OnItemDataBound?
Heres my repeater:
<asp:Repeater ID="newsListRepeater" runat="server" OnItemDataBound="deleteConfirm_Databound">
<ItemTemplate>
<tr>
<td><%# Eval("id") %></td>
<td><%# Eval("title") %></td>
<td><%# Eval("tags") %></td>
<td><%# Eval("author") %></td>
<td style="width:100px;"><%# DataBinder.Eval(Container.DataItem, "time", "{0:dd/MM/yyyy}") %></td>
<td style="width:110px;">
<asp:Button ID="editNewsButton" runat="server" OnCommand="editNewsButton_Click" CommandArgument='<%# Eval("id") %>' Text="Rediger" />
<asp:Button ID="deleteNewsButton" runat="server" CommandArgument='<%# Eval("id") %>' Text="Slet" />
</td>
</tr>
</ItemTemplate>
</asp:Repeater>
EDIT:
How would i bind a paramter from behind code, to my 'deleteNewsButton' controllers.
It needs to be done from behind code...
I believe for a repeater item databound event you need to use RepeaterItemEventArgs instead of RepeaterItem
protected void deleteConfirm_Databound(object sender, RepeaterItem e)
becomes
protected void deleteConfirm_Databound(object sender, RepeaterItemEventArgs e)
You might also want a null check for that control:
LinkButton button = (LinkButton)e.FindControl("deleteNewsButton");
if(button != null) {
button.Attributes.Add("onclick", "javascript:return " +
"confirm('Er du sikker på du vil slette: " + DataBinder.Eval(e.DataItem, "id") + "')");
}
You may also consider making a common javascript function for the confirm box, and then just calling it from your button.
aspx
<script type="text/javascript">
function confirmFunction(id)
{
return confirm('Er du sikker på du vil slette: ' + id);
}
</script>
codebehind
LinkButton button = (LinkButton)e.FindControl("deleteNewsButton");
if(button != null) {
button.Attributes.Add("onclick", "javascript:return confirmFunction(" + DataBinder.Eval(e.DataItem, "id") + ");");
}
add an onclientclick event eg.
<asp:Button ID="deleteNewsButton"
OnClientClick="return confirm('Do you really want to Delete this record ?')"
runat="server" CommandArgument='<%# Eval("id") %>' Text="Slet" />