Check on Page_Load which item was clicked on a ResultsRepeater - c#

I know this is a pretty strange question but I need to know which item was clicked before the click event fires on the repeater.
This will tell me that it was the repeater that is doing the post back, but not which item was clicked:
Request.Params["__EVENTTARGET"].ToString()
How do I find out which repeater item was clicked?
If it helps, here is the markup for the repeater in full.
<asp:Repeater ID="ResultsRepeater" runat="server">
<HeaderTemplate>
<table cellpadding="3" cellspacing="0" width="360">
<tr bgcolor="#d5d5e6">
<th width="10">
</th>
<th>
Content Type
</th>
<th>
Items Found
</th>
<th>
Results
</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr bgcolor="#efefef">
<td width="10">
<img src="http://www.exactmobile.co.za/images/li_x.gif">
</td>
<td>
<%# DataBinder.Eval(Container, "DataItem.Name") %>
</td>
<td align="center">
<b class="brown">
<%# DataBinder.Eval(Container, "DataItem.Count") %></b>
</td>
<td align="center">
<asp:LinkButton runat="server" CommandName="Results">Show <font class="red">»</font></asp:LinkButton>
</td>
</tr>
</ItemTemplate>
<AlternatingItemTemplate>
<tr bgcolor="#ffffff">
<td width="10">
<img src="http://www.exactmobile.co.za/images/li_x.gif">
</td>
<td>
<%# DataBinder.Eval(Container, "DataItem.Name") %>
</td>
<td align="center">
<b class="brown">
<%# DataBinder.Eval(Container, "DataItem.Count") %></b>
</td>
<td align="center">
<asp:LinkButton runat="server" CommandName="Results">Show <font class="red">»</font></asp:LinkButton>
</td>
</tr>
</AlternatingItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>

Did you try setting up event handlers for each of your link button controls? If not, doing that might as well take you exactly to respective event handler automatically.
Also see if this helps ASP.NET - Add Event Handler to LinkButton inside of Repeater in a RenderContent call

Worked around the issue by just using normal anchors in the template and setting their hrefs to the actual pages in the controls Pre_Render method.

Related

Enable to edit after a checkbox

I have a table with some information shown, but I would like to able to edit after I click the checkbox above.
<asp:Button ID="Backbtn" runat="server" Text="Back" Width="84px"
onclick="Backbtn_Click" />
<br /><br />
<asp:CheckBox ID="CheckBox1" runat="server" onclick="check" Text="Check"/>
<table class="T-Table" >
<tr>
<th colspan="6"><em>IT INTERNAL USE</em></th>
</tr>
<tr>
<td width="180px">Doc.No </td>
<td>:</td>
<td width="160px">
<asp:Label ID="DocNo" runat="server" Text="Label"></asp:Label>
</td>
<%-- <td width="180px">Job Complete Date </td>
<td>:</td>
<td width="160px">
</td> --%>
</tr>
<tr>
<td>Received Date :</td>
<td>:</td>
<td>
<asp:Label ID="RDate" runat="server" Text="Label"></asp:Label>
</td>
<td>Response Time (HR) :</td>
<td>:</td>
<td><asp:TextBox ID="Response_T" runat="server" TextMode="DateTimeLocal"></asp:TextBox>
</td>
</tr>
<tr>
<td>Remarks </td>
<td>:</td>
<td colspan="5"><asp:TextBox ID="Remarks" runat="server" Rows="5" TextMode="MultiLine"
Columns="80"></asp:TextBox></td>
</tr>
<tr>
<td>Cost charge to Department </td>
<td>:</td>
<td><asp:TextBox ID="Cost" runat="server" ></asp:TextBox></td>
<td>Status</td>
<td>:</td>
<td> <asp:DropDownList ID="Progress" runat="server">
<asp:ListItem Text="Done" Value="Done"></asp:ListItem>
<asp:ListItem Text="In Progress" Value="In Progress"></asp:ListItem>
<asp:ListItem Text="Pending" Value="Pending"></asp:ListItem>
</asp:DropDownList></td>
</tr>
Here are some html code. I would like to add a function so that all the information can be editable after I click the Checkbox.
Thank you
There is nothing to do with C# or Linq here (as you tagged).
Assuming you are working on an ASP.NET application
Enable the checkbox AutoPostback. ie, Set AutoPostback property of the checkbox to true.
Then on page load or checkbox checked change event, enable the textbox.
eg. name of text editor is txtRemarks and checkbox name to be chkRemarksEditor
on chkRemarksEditor Checked change event :-
txtRemarks.Enabled = chkRemarksEditor.Checked;

How to debug an aspx page, keeps saying page not found when there is an error

I have a page with a listview and there is some problem with it, however instead of getting an error, when I run it I get a 404 resource not found error. If I remove the offending listview and run then it runs fine, so it is not an issue with it not looking in the right place for the page etc.
How can I find out what the error is with the listview, at the moment I'm getting no information about it, it even renders correctly in design view.
Many thanks
For info the code is below, however I need to know how to debug it as this has happened lots of times before.
If I remove this listview the page works..
<asp:ListView ID="lvPerformanceKPIs" runat="server"
OnItemDataBound="lvPerformanceKPIs_ItemDataBound" DataKeyNames="PerformanceMeasureID"
OnItemCommand="lvPerformanceKPIs_ItemCommand" ItemPlaceholderID="itemPlaceholder"
>
<LayoutTemplate>
<table width="100%" border="0" cellpadding="2" cellspacing="0">
<tr>
<th width="5%" class="Actions" >
Actions
</th>
<th width="2%" >&nbsp
</th>
<th width="5%">
ID
</th>
<th width="28%">
Group
</th>
<th width="40%">
Indicator
</th>
<th width="20%">
Threshold
</th>
</tr>
</table>
<div runat="server" id="itemPlaceHolder">
</div>
</LayoutTemplate>
<ItemTemplate>
<div id="Div1" class="SUBDIV" runat="server">
<table width="100%" border="0" cellpadding="2" cellspacing="0">
<tr>
<td width="5%" class="Actions">
<%--<img id="btnEdit" style="cursor: pointer;" alt="Edit" title="Edit this record" src="images/edit.png" width="15px"
onclick="CRMEdit(this);"/>
<asp:ImageButton ID="btnDelete" runat="server" title="Delete this record" src="images/delete.png" width="15px"
OnclientClick="return confirm('Are you sure you want to delete this record?')"
CommandName="cmdDelete" CommandArgument=<%#Eval("PerformanceMeasureID") %> /> --%>
</td>
<td width="2%">
<div class="btncolexp collapse" title="Expand to reveal/add files">
</div>
</td>
<!-- id columns to get values then will be hidden with jquery -->
<td class="hidCol">
<span id="itemServiceSpecID"><%#Eval("ServiceSpecID")%></span>
</td>
<td class="hidCol">
<span id="itemPerfSortOrder"><%#Eval("PerfSortOrder")%></span>
</td>
<td class="hidCol">
<span id="itemPerfType"><%#Eval("PerfType")%></span>
</td>
<td class="hidCol">
<span id="itemPerfFrequency"><%#Eval("PerfFrequency")%></span>
</td>
<td width="5%">
<span id="itemPerformanceMeasureID"><%#Eval("PerformanceMeasureID")%></span>&nbsp
</td>
<td width="28%">
<span id="itemPerfGroup"><%#Eval("PerfGroup")%></span>&nbsp
</td>
<td width="40%">
<span id="itemPerfMeasure"><%#Eval("PerfMeasure")%></span>&nbsp
</td>
<td width="20%">
<span id="itemPerfThresholdText"><%#Eval("PerfThresholdText")%></span>&nbsp
</td>
</tr>
</table>
</div>
</ItemTemplate>
</asp:ListView>
i wonder if those images are properly commented-out in the item template. make sure there is no conflict between the comment tags (<%-- --%>) and the data binding tags.
also, the CommandArgument needs to be quoted:
CommandArgument='<%#Eval("PerformanceMeasureID") %>'

asp.net how to pass list of items to eval function?

this is my code to view products as list view inside each item in list view i want to view list of items how can i do this? i can not understand the concept of the eval function and i want to know if i can pass to it list items from code behind without data Bind or it basically depends on data-bind ?
<asp:ListView ID="mylistView" runat="server" GroupItemCount="3">
<EmptyDataTemplate>
<table id="Table2" runat="server">
<tr>
<td>
No data was returned.
</td>
</tr>
</table>
</EmptyDataTemplate>
<EmptyItemTemplate>
<td id="Td3" runat="server" />
</EmptyItemTemplate>
<GroupTemplate>
<tr id="itemPlaceholderContainer" runat="server">
<td id="itemPlaceholder" runat="server">
</td>
</tr>
</GroupTemplate>
<ItemTemplate>
<table border="0" width="300" style="display: inline-block; background-color: Lime;">
<tr>
<td>
&nbsp
</td>
<td>
<a href='ContactUs.aspx'>
<image src='productsImg/<%# Eval("Pic_Path") %>' width="100" height="75" border="0">
</a>&nbsp
</td>
<td>
<a><b style="text-decoration: underline;">
<%# Eval("MenuName") %></b> </a>
<br />
want to view here list of items
I
</td>
</tr>
</table>
</ItemTemplate>
<LayoutTemplate>
<table id="Table1" runat="server">
<tr id="Tr1" runat="server">
<td id="Td1" runat="server">
<table id="groupPlaceholderContainer" runat="server">
<tr id="groupPlaceholder" runat="server">
</tr>
</table>
</td>
</tr>
<tr id="Tr2" runat="server">
<td id="Td2" runat="server">
</td>
</tr>
</table>
</LayoutTemplate>
</asp:ListView>
this is code behind...
dataSource.ConnectionString = connetionString;
dataSource.SelectCommand = "SELECT * FROM tbl_WebMenu where MenuID like'3_';";
mylistView.DataSource = dataSource;
mylistView.DataBind();
thanks in advance.
Eval is used to bind to an UI control that is supposedly read only. it can be used to set any type of property of control.
The complete syntax is "Databinder.Eval", it has to be used in conjunction with databind.
Look at this example.
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="lblRead" runat="server" Text='<%# Eval("FieldName") %>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
you can even set a property of control using eval. it provides flexibility.
But there is a downside of eval as it uses reflection to evaluate the expression.
Read more here http://msdn.microsoft.com/en-us/library/4hx47hfe.aspx#remarksToggle

Redirect on Listview row click

I have a Listview and one of the columns is called id_Number.
Is it possible to redirect to another page, if user clicks on one of the rows?
For example if id_Number is 4, than page should be redirected to ~/Page.aspx?id=4.
Code:
<asp:ListView ID="ListView1" runat="server"
DataKeyNames="id_Number" >
<EmptyDataTemplate>
<table id="Table1" runat="server" style="">
<tr>
<td>
No data was returned.</td>
</tr>
</table>
</EmptyDataTemplate>
<ItemTemplate>
<tr style="font-family: Arial; color: #FFFFFF" onMouseOver="this.bgColor='#219DD0';" onMouseOut="this.bgColor='#252526';">
<td>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("id_Number") %>' />
</td>
<td>
<asp:Label ID="TextLabel" runat="server" Text='<%# Eval("Text") %>' />
</td>
<td>
<asp:Label ID="DateLabel" runat="server" Text='<%# Eval("Date") %>' />
</td>
</tr>
</ItemTemplate>
<LayoutTemplate>
<table id="Table2" runat="server">
<tr id="Tr1" runat="server">
<td id="Td1" runat="server">
<table ID="itemPlaceholderContainer" runat="server" border="0" style="">
<tr id="Tr2" runat="server" style="">
<th id="Th1" runat="server">
Name</th>
<th id="Th2" runat="server">
Text</th>
<th id="Th3" runat="server">
Date</th>
</tr>
<tr ID="itemPlaceholder" runat="server">
</tr>
</table>
</td>
</tr>
</table>
</LayoutTemplate>
</asp:ListView>
You can add an HTML link or a JavaScript into the ItemTemplate of your ListView pretty easily:
The "columns" are just HTML columns; in the ItemTemplate, you have access to all of your data, including id_number, so you can use it anywhere in the HTML, not just inside a column.
Since you wan the whole row to be clickable, I'd use a JavaScript function on the onClick event of the HTML table row <TR> element. You already have 2 events (onMouseOver, onMouseOut), with JavaScript in them, so this would just be one more...
A JavaScript redirect utilizes window.location.href which will redirect in the same window, assuming that's what you want (as opposed to a new window, etc.):
<ItemTemplate>
<tr style="font-family: Arial; color: #FFFFFF"
onMouseOver="this.bgColor='#219DD0';"
onMouseOut="this.bgColor='#252526';"
onClick="window.location.href = 'some/path/Page.aspx?id=<%# Eval("id_Number") %>';"
<td>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("id_Number") %>' />
</td>
<td>
<asp:Label ID="TextLabel" runat="server" Text='<%# Eval("Text") %>' />
</td>
<td>
<asp:Label ID="DateLabel" runat="server" Text='<%# Eval("Date") %>' />
</td>
</tr>
</ItemTemplate>
One thing to note is for the path of Page.aspx, you have to use a relative path.
~/page.aspx?id=
Is a .net path that gets compiled into the full relative path to that page.
However, bacause your ItemTemplate contains HTML, you'd need the full (relative) path to Page.aspx in there.
So instead it should be (just an example):
/some/path/page.aspx?id= <-- page.aspx lives on http://www.yoursite.com/some/path/page.aspx
/page.aspx?id= <-- page.aspx lives on root, http://www.yoursite.com/page.aspx
Alternately you can use
onClick="window.location.href = '<% System.Web.VirtualPathUtility.ToAbsolute("~/Page.Aspx?ID=" + Eval("id_number")); %>';"

Retrofit plain HTML grid to c# grid

I realize this is a strange/simple question. I have HTML—tables and rows—that make up a grid. I need the resulting grid to look exactly like the HTML does, with data pulled from a DataTable.
How should I go about doing that? If I replace the HTML with a GridView the result will look different from what I need it to look like. Do I create a custom grid class that spits out tables and rows?
To clarify: I've been given an HTML grid...
<div>
<table>
<tr>
<td>row 1 col 1</td>
<td>row 1 col 2</td>
</tr>
<tr>
<td>row 2 col 1</td>
<td>row 2 col 2</td>
</tr>
</table>
</div>
...which I now need to populate with data from a table, and preserve the original HTML.
If you want to use ASP.Net databinding capabilities, go with a Repeater. The markup will look something like this:
<table>
<thead>
<tr>
<th>Heading</th>
</tr>
</thead>
<tbody>
<asp:Repeater runat="server">
<ItemTemplate>
<tr>
<td><%# Eval("SomeFieldName") %></td>
</tr>
</ItemTemplate>
</asp:Repeater>
</tbody>
</table>
You can also build the markup manually in the code-behind by instantiating table/row/cell objects and adding them to the controls collection of the page, control, or parent container control. This tends to be time-consuming but it's a valid option in some cases, such as when you wish to have highly dynamic content that isn't easy to express in a Repeater.
A third option is to mix server markup with client markup, e.g.
<table>
<thead>
<tr>
<th>Heading</th>
</tr>
</thead>
<tbody>
<% foreach( var obj in someCollection ){ %>
<tr>
<td><%= obj.Property %></td>
</tr>
<% } %>
</tbody>
</table>
The last option is quite similar (albeit more verbose) to Razor syntax used in ASP.Net MVC views.
I think your best bet for that would be a repeater. Link below.
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.repeater(v=vs.100).aspx
Another option would be to use a ListView with your HTML table setup as the LayoutTemplate.
quick example:
<asp:ListView ID="MyListView" runat="server">
<LayoutTemplate>
<table id="Table1" cellpadding="1" width="100%" runat="server">
<tr id="Tr1" runat="server">
<th id="Th1" style="font-weight: bold; text-align: left" runat="server">
Header1
</th>
<th id="Th2" style="font-weight: bold; text-align: left" runat="server">
Header2
</th>
</tr>
<tr runat="server" id="itemPlaceholder" />
</table>
</LayoutTemplate>
<ItemTemplate>
<tr id="Tr2" runat="server">
<td style="text-align: left">
<%# Eval("Field1")%>
</td>
<td style="text-align: left">
<%# Eval("Field2")%>
</td>
</tr>
</ItemTemplate>
</asp:ListView>

Categories