I've inherited an ASP.NET project that has poorly designed HTML; in one section, the <TR> tags are wrapped with an tag to allow for "click the row to view the information" functionality. The code is:
<asp:LinkButton ID="ViewArticle" runat="server" CommandName="Navigate" CommandArgument='<%# DataBinder.Eval(Container.DataItem, "id") %>' >
<tr runat="server" class="list_item">
<td>some data</td>
<td>some data</td>
<td>some data</td>
</tr>
</asp:LinkButton>
I'd like to do something like:
<tr runat="server" class="list_item" onclick="doRowPostbackFunction('<%# DataBinder.Eval(Container.DataItem, "id") %>');">
<td>some data</td>
<td>some data</td>
<td>some data</td>
</tr>
How do I go about tying a JavaScript onclick event to the codebehind?
Using jQuery you can do the following and trigger a postback:
<table>
<tr id="1">
<td>AAA</td>
<td>BBB</td>
<td>CCC</td>
</tr>
<tr id="2">
<td>AAA</td>
<td>BBB</td>
<td>CCC</td>
</tr>
<tr id="3">
<td>AAA</td>
<td>BBB</td>
<td>CCC</td>
</tr>
</table>
<script type="text/javascript">
$(function () {
$("table tr").click(function (e) {
//alert the row index that was clicked
alert($("table tr").index(this));
//alert the id of the row
alert($(this).attr("id"));
//submit the form...
});
});
</script>
OR use the onclick event of the row...
<tr onclick="doPostBack('<%#DataBinder.Eval(Container.DataItem, "id") %>')">
...and trigger a javascript function.
<script type="text/javascript">
function doPostBack(id)
{
//submit form
}
</script>
You could make your page (or control, if that's inside a control) implement IPostBackEventHandler interface and use ClientScriptManager.GetPostBackEventReference method (using Page.ClientScript propery to get a reference to ClientScriptManager) to get a js-method-call-string which will make a postback to your page. As a control argument to GetPostBackEventReference method use your page.
Then set onclick property of tr to that js-method-call-string.
Example
In aspx:
<table>
<tr onclick="<%= _jsPostBackCall %>;">
<td>a</td>
<td>b</td>
<td>c</td>
</tr>
</table>
<h1>
<asp:Literal ID="litMessage" runat="server" />
</h1>
In codebehind:
public partial class _Default : System.Web.UI.Page, IPostBackEventHandler
{
protected String _jsPostBackCall;
protected void Page_Load(object sender, EventArgs e)
{
// "myTr" will be passed as an argument to RaisePostBackEvent method
_jsPostBackCall = ClientScript.GetPostBackEventReference(this, "myTr");
}
public void RaisePostBackEvent(string eventArgument)
{
switch (eventArgument)
{
case "myTr":
HandleTrClick();
break;
// you can add other controls that need postback processing here
default:
throw new ArgumentException();
}
}
private void HandleTrClick()
{
litMessage.Text = "Tr clicked.";
}
}
You can simply use
__doPostBack(eventName, eventArgs);
Here eventName is code-behind event handler
Related
In my masterpage I have links for users also the authorization is different like admin and regular user.
links under each other and I can hide the hyperlinks depending on authorization status but the problem is i.e when I have 3 links the second link for the admin the link will hide when the user is regular and the link place empty like 123 1 3.
So I have an idea using table each link in one tr but I can`t hide td or tr because Visible is not in properties.
any help?
thank you
According to how to hide a having asp.net control:
you can give ID either to the TD or TR to which you want to hide/show
with the runat="server" and also you can take that tr/td inside the
div tag and give id to that div tag and also runat=server attribute
and after that you can pro grammatically hide/show that div.
like
<pre>
<tr id="trhide" runat="server"> </tr>
</pre>
in code behind write
trhide.visible=true/false
In the master page VB code behind add a public procedure: Then call the public set from your aspx page.
'======================================================================================================
'Set Tab No invisible
'======================================================================================================
Public Sub setTabNumberLabel(visible As Int16)
If visible = 0 Then
td_page.Visible = False
Else
td_page.Visible = True
End If
End Sub
The master aspx would be:
<table style="width:100%">
<!--<tr style="background-color:#565656;">-->
<tr>
<td style="width:15%;text-align:left;vertical-align:bottom;padding-left:20px;">Stategic Energy Assessment ( <asp:Label ID="lbl_year_ended" runat="server" /> )</td>
<td style="text-align:center;vertical-align:bottom;"><asp:Label ID="lbl_utility_name_and_id" runat="server" /></td>
<td id="td_page" runat="server" style="width:15%;text-align:right;vertical-align:bottom;padding-right:20px;">Tab No: <asp:Label ID="lbl_page" runat="server" /></td>
</tr>
<tr><td colspan="3" style="vertical-align:central"><hr /></td></tr>
<tr>
<td style="width:15%;text-align:left;vertical-align:central">
<asp:Label ID="lbl_print_version" runat="server" Text="View Printable Vision" Visible="false" />
</td>
<td style="font-size:larger; font-weight:bold; text-align:center; text-transform:capitalize;vertical-align:central">
<asp:Label ID="lbl_schedule_name" runat="server" />
</td>
<td style="width:15%;text-align:right;vertical-align:central;padding-right:20px;">
<asp:LinkButton ID="btn_footnotes" runat="server" Visible="false">Footnotes</asp:LinkButton>
</td>
</tr>
<%--<tr><td colspan="3" style="vertical-align:central" class="auto-style1"></td></tr>--%>
<tr><td colspan="3" style="vertical-align:central; padding-right:20%;padding-left:20%; ">
<i><asp:Label ID="lbl_headnotes" runat="server" Text="" /></i></td></tr>
<tr><td colspan="3" style="vertical-align:central"><hr /></td></tr>
</table>
The other answer is correct and works fine. Just adding complete piece of code.
It's quite amusing that you don't need to add runat=server for a table but you can still hide tr for that table using runat attribute.
<table>
<tr>
<td>aa</td><td>bb</td>
</tr>
<tr id="trHide1" runat="server">
<td>aa</td><td>bb</td>
</tr>
<tr id="trHide2" runat="server">
<td>aa</td><td>bb</td>
</tr>
<tr>
<td>aa</td><td>bb</td>
</tr>
</table>
Now just set properties in codebehind (hiding the tr)
trHide1.Visible = false;
trHide2.Visible = false;
I have a repeater. And i want to hide and display a particular column for a particular condition. I have three types of subjects and their ids are 0,1,2 respectively. Now i want to show that particular column when the subject will be 2 only..
My code is :-
<table id="table1" class="yui" cellpadding="0" cellspacing="0">
<thead>
<tr>
<th>
EmpID #
</th>
<th>Edit</th>
</tr>
</thead>
<tbody>
<asp:Repeater ID="Repaddressorbbl" runat="server" OnItemCommand="Repaddressorbbl_ItemCommand">
<ItemTemplate>
<tr id="gh" style="cursor: pointer" onclick="Select(this);">
<td style="text-align: center;">
<%#Eval("empid")%>
</td>
<td>
<asp:LinkButton ID="lknumber" runat="server" Text="Edit" CommandName="subjectid" />
</td>
</tr>
</ItemTemplate>
</asp:Repeater>
</tbody>
<tfoot>
</tfoot>
</table>
You could catch the OnItemDataBound event of the repeater and hide the column there if the (subject) item id is 2.
In order you can get a reference to the column, make it a server control:
<td style="text-align: center;" id="COL_TO_HIDE" runat="server"><%#Eval("empid")%></td>
Then in the repeater event you can simply look for the control and hide it:
protected void YourRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
var subject = (Subject)e.Item.DataItem;
if (subject.Id == 2)
{
var col = e.Item.FindControl("COL_TO_HIDE");
col.Visible = false;
}
}
}
Please note, this is just a simplified example which should you get started.
I think you should start by using <HeaderTemplate></HeaderTemplate> and <FooterTemplate></FooterTemplate> to define the start and end of your table just to tidy it up.
You can get the table to run on the server by adding a runat="server" and give the column <td> an id and a runat="server" attribute so you can program server code against it. I'd then eval bind the visible attribute of the cell based on your field value or use attributes.add("display:none") or just use a grid view as suggested in the link.
<asp:Repeater ID="Repaddressorbbl" runat="server"
OnItemCommand="Repaddressorbbl_ItemCommand">
<ItemTemplate>
<tr id="gh" style="cursor: pointer" onclick="Select(this);">
<td style="text-align: center;">
<%#Eval("empid")%>
</td>
<% if (false){ %>
<td>
<asp:LinkButton ID="lknumber" runat="server"
Text="Edit" CommandName="subjectid" />
</td>
<% } %>
</tr>
</ItemTemplate>
</asp:Repeater>
I am getting this error by simply trying to insert some VB.NET code in ASP.NET markup. See the code:
<%# Control Inherits="PerformanceWeb.Framework.SiteSettings" CodeBehind="sitesettings.ascx.vb" language="vb" AutoEventWireup="false" %>
<table id="TABLE1" cellSpacing="0" cellPadding="2" border="0" runat="server">
<% If EditDowntimeMode Then%>
<tr><td class="Normal"><asp:label id="lblDowntimeLegacyMode" Runat="server">lblDowntimeLegacyMode</asp:label></td></tr>
<tr>
<td class="DowntimeLegacyModeIndented" width="130">
<asp:label id="lblLegacyMode" Runat="server">lblLegacyMode</asp:label>
</td>
<td class="Normal" colSpan="2">
<asp:RadioButton id="rdoLegacyMode" GroupName="DowntimeLegacyMode" Runat="server"></asp:RadioButton>
</td>
</tr>
<tr>
<td class="DowntimeLegacyModeIndented" width="130">
<asp:label id="lblNewCauses" Runat="server">lblNewCauses</asp:label>
</td>
<td class="Normal" colSpan="2">
<asp:RadioButton id="rdoNewCauses" GroupName="DowntimeLegacyMode" Runat="server"></asp:RadioButton>
</td>
</tr>
<tr>
<td colspan="2"><hr /></td>
</tr>
<% End if%>
</table>
Codebehind
#Region "Properties"
Public Property EditDowntimeMode() As Boolean
Get
Return m_EditDowntimeMode
End Get
Set(ByVal value As Boolean)
m_EditDowntimeMode = value
End Set
End Property
#End Region
When you add a runat='server' to an HTML control you change the rendering and code blocks aren't supported inside.
Changed from:
<table id="TABLE1" cellSpacing="0" cellPadding="2" border="0" runat="server">
to:
<table id="TABLE1" cellSpacing="0" cellPadding="2" border="0">
In the code behind on the Page Load, couldn't you just set the table's visible property to the negation of EditDowntimeMode ? That would seem to be a better solution than trying to mix all that markup together in what you are doing.
Something like this in the Page_Load method of the code behind:
Table1.Visible = Not EditDowntimeMode
The given example code in the question should be updated if that is the case as the example code has the if blocking out everything within the table. You could use an ASP:Panel block and control its visibility for another idea or possibly nest the tables so that an inner table could be just what the "EditDowntimeMode" would show is in its own table.
I have a table with 5 rows and 2 columns. Each cell contains a text box. I want to show error if one of the text boxes in each column is empty. I want both text boxes in a row shld be filled or both shld empty.
How can i do this via Asp.net validation controls?
I want to extend CompareValidator so that it will validate only if the controlToValidate and controlToCompare both have some text in it or both are empty.
You would need to use a CustomValidator and handle its ServerValidate event (and, optionally, its ClientValidationFunction for client-side validation). You could do one on the page and check all the rows, or you could have one per row and use the ControlToValidate property to give you context to the row you're validating.
Any example of the client-side validation is going to depend on your layout and any JavaScript framework that you're using. It might look something like this:
<table>
<tr>
<td><asp:TextBox runat="server" ID="TextBox11" /></td>
<td><asp:TextBox runat="server" ID="TextBox12" /></td>
</tr>
<tr>
<td><asp:TextBox runat="server" ID="TextBox21" /></td>
<td><asp:TextBox runat="server" ID="TextBox22" /></td>
</tr>
<tr>
<td><asp:TextBox runat="server" ID="TextBox31" /></td>
<td><asp:TextBox runat="server" ID="TextBox32" /></td>
</tr>
<tr>
<td><asp:TextBox runat="server" ID="TextBox41" /></td>
<td><asp:TextBox runat="server" ID="TextBox42" /></td>
</tr>
<tr>
<td><asp:TextBox runat="server" ID="TextBox51" /></td>
<td><asp:TextBox runat="server" ID="TextBox52" /></td>
</tr>
</table>
<asp:CustomValidator ID="TextBoxPairValidator" runat="server" ControlToValidate="TextBox11" ClientValidationFunction="TextBoxPairValidator_ClientValidate" />
<script type="text/javascript">
(function () {
window.TextBoxPairValidator_ClientValidate = function (sender, args) {
var other = document.getElementById(sender.id.slice(0, -1) + '2');
args.IsValid = (sender.value === '' && other.value === '')
|| (sender.value !== '' && other.value !== '');
};
}());
</script>
That example obviously assumes a fairly simple layout, and fairly static naming (i.e. if your controls are in a naming container, you may not be able to use the ID trick to go from one text box to the other). Hopefully that's enough to get you started.
I am currently having an issue with radio buttons and grouping. I have an asp radio button within a repeater control. I have the group name attribute set to "Customer". When the page loads, the radio buttons are not grouped. Instead of the id fields being set to the group name, it is setting the value fields of the radio buttons. I know that I have tried setting radio buttons up outside of a repeater control and have had the same issue. What is going on here?
aspx
<asp:Repeater ID="repCustomers" runat="server">
<HeaderTemplate>
<table class="tableDefault" cellpadding="0" cellspacing="0" border="0" style="width: 383px; border: 0px !important">
<tr>
<th> </th>
<th>Cust. No.</th>
<th>Cust. Name</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<asp:RadioButton ID="radCustomer" GroupName="Customer" runat="server" ValidationGroup="Customer" ToolTip='<%#Eval("CustomerNumber") %>' />
</td>
<td><%#Eval("CustomerNumber")%></td>
<td><%#Eval("Name") %></td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
output html
<table class="tableDefault" cellpadding="0" cellspacing="0" border="0" style="width: 383px; border: 0px !important">
<tr>
<th> </th>
<th>Cust. No.</th>
<th>Cust. Name</th>
</tr>
<tr>
<td>
<span title="111111"><input id="ctl00_PrimaryContent_repCustomers_ctl01_radCustomer" type="radio" name="ctl00$PrimaryContent$repCustomers$ctl01$Customer" value="radCustomer" /></span>
</td>
<td>111111</td>
<td>Jeremy's Test</td>
</tr>
<tr>
<td>
<span title="222222"><input id="ctl00_PrimaryContent_repCustomers_ctl02_radCustomer" type="radio" name="ctl00$PrimaryContent$repCustomers$ctl02$Customer" value="radCustomer" /></span>
</td>
<td>222222</td>
<td>My Test</td>
</tr>
<tr>
<td>
<span title="333333"><input id="ctl00_PrimaryContent_repCustomers_ctl03_radCustomer" type="radio" name="ctl00$PrimaryContent$repCustomers$ctl03$Customer" value="radCustomer" /></span>
</td>
<td>333333</td>
<td>Jim Bob's BBQ</td>
</tr>
<tr>
<td>
<span title="444444"><input id="ctl00_PrimaryContent_repCustomers_ctl04_radCustomer" type="radio" name="ctl00$PrimaryContent$repCustomers$ctl04$Customer" value="radCustomer" /></span>
</td>
<td>444444</td>
<td>New Hope Hamburgers</td>
</tr>
<tr>
<td>
<span title="555555"><input id="ctl00_PrimaryContent_repCustomers_ctl05_radCustomer" type="radio" name="ctl00$PrimaryContent$repCustomers$ctl05$Customer" value="radCustomer" /></span>
</td>
<td>555555</td>
<td>Pied Piper Pizza</td>
</tr>
<tr>
<td>
<span title="666666"><input id="ctl00_PrimaryContent_repCustomers_ctl06_radCustomer" type="radio" name="ctl00$PrimaryContent$repCustomers$ctl06$Customer" value="radCustomer" /></span>
</td>
<td>666666</td>
<td>Sandy's Subs</td>
</tr>
<tr>
<td>
<span title="777777"><input id="ctl00_PrimaryContent_repCustomers_ctl07_radCustomer" type="radio" name="ctl00$PrimaryContent$repCustomers$ctl07$Customer" value="radCustomer" /></span>
</td>
<td>777777</td>
<td>Leonard's Lambchops</td>
</tr>
<tr>
<td>
<span title="888888"><input id="ctl00_PrimaryContent_repCustomers_ctl08_radCustomer" type="radio" name="ctl00$PrimaryContent$repCustomers$ctl08$Customer" value="radCustomer" /></span>
</td>
<td>888888</td>
<td>Dave's Diamond Deli</td>
</tr>
<tr>
<td>
<span title="999999"><input id="ctl00_PrimaryContent_repCustomers_ctl09_radCustomer" type="radio" name="ctl00$PrimaryContent$repCustomers$ctl09$Customer" value="radCustomer" /></span>
</td>
<td>999999</td>
<td>Ernie's Eatery</td>
</tr>
</table>
I finally got around this by creating a plain radio button and setting the value using an server-side eval.
<input type="radio" name="radCustomer" value='<%#Eval("CustomerNumber") %>' />
Now when the application performs a postback, I check for the value of Request.Form["radCustomer"]. This works flawlessly.
Unfortunately, this is a well known issue with radio buttons within a repeater. One of your only options would be to create a custom server control derived from the RadioButton class and override how it renders.
EDIT: Here's a sample of what the derived class may look like:
public class MyRadioButton : RadioButton
{
protected override void Render(HtmlTextWriter writer)
{
writer.Write("<input id=\"" + base.ClientID + "\" ");
writer.Write("type=\"radio\" ");
writer.Write("name=\"" + base.ID + "\" ");
writer.Write("value=\"" + base.ID + "\" />");
writer.Write("<label for=\"" + base.ClientID + "\">");
writer.Write(base.Text);
writer.Write("</label>");
}
}
I fixed it in javascript:
$(function () {
$("#divWithGridViewOrRepeater input:radio").attr("name", "yourGroupName");
});
I had the same issues. I am using Literal as placeholder to render radio button onItemCreated event.
ASP.Net
<asp:Repeater ID="rpt" runat="server" OnItemCreated="rpt_OnItemCreated">
<ItemTemplate>
<asp:Literal ID="lit" runat="server"></asp:Literal>
</ItemTemplate>
</asp:Repeater>
C#
protected void rpt_OnItemCreated(object sender, RepeaterItemEventArgs e) {
Literal lit = (Literal)e.Item.FindControl("lit");
lit.Text = "<input type=\"radio\" name=\"myGroup\">";
}
I had to modify slightly the answer posted above by r3dsky.
Here's what worked for me:
$(document).ready(function () {
$(".divWithGridViewOrRepeater input:radio").attr("name", "yourGroupName");
});
I would start by adding a value on my radiobutton Value='<%#Eval("CustomerNumber") %>'.
I made my radiobutton have autopostback set to true, and then in the event handler set all the other radio buttons to BE unselected.
Not ideal, but I need lots control over the visibility and enabled attributes of the radiobutton, and it seemed easier to let ASP.NET control that rather than resorting to client side script.
This is a well known bug with the ASP.NET Repeater using RadioButtons:
here best solution in my opinion
I did this:
$("input:radio").attr("name", $("input:radio").first().attr("name"));
Why? because if you replace the name property for any string you want, you will get an 'not found error'. So, you need to get the name of the first radiobutton, and rename all of them with that name. It works like a sharm ;)
My solution, similar to others:
<input id="ctlRadio" runat="server" type="radio" data-fixgroupbug="1" >
// Fixes this ASP.NET bug: if radio input is inside repeater you can't set its name.
// Every input gets set different name by ASP.NET.
// They don't behave as a group. You can select multiple radios.
function fixRadiogroupBug()
{
$('[type="radio"][data-fixgroupbug]').click(function () {
$(this).siblings('[type="radio"]').prop('checked', false);
});
}
$(document).ready(function () {
fixRadiogroupBug();
});