Check li click value - c#

I m using two list items with link. Now I want to check which link was clicked from the client side. Here is my code:
<ul id="navtabs">
<li id="basic" runat="server" class="cat-item cat-item-6 current-cat">new</li>
<li id="new" runat="server" class="cat-item cat-item-8">old</li>
</ul>
I want to check which item am click whether basic or new
if(this.basic.clicked ==true)
{
do something
}
else
{
do something
}
How can I do this?

If you are suing Web forms then,
The the control which are suing must be server control
If it is a link the put runat="server" and id of the control
Create on click event of every link.
But what is the purpose of those link is not clear.
What action do you want to take on the click must be clear.

I have a trick for it. You can do this with help of jQuery, one hidden field & one hidden asp button.
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Hidden Button" />
<asp:HiddenField ID="hdnLIClicked" runat="server" />
Now jQuery part, in ready event
$().ready(function() {
$("#Button1").hide();//hiding the button through jQuery
$("#basic").click(function(){
$("#hdnLIClicked").val("basic");
$("#Button1").click();
});
$("#new").click(function(){
$("#hdnLIClicked").val("new");
$("#Button1").click();
});
});
Or you can give a common class to all your list items, if there are more list items, like you already have a common class for both of your list items. i.e "cat-item".
$().ready(function() {
$("#Button1").hide();//hiding the button through jQuery
$(".cat-item").click(function(){
$("#hdnLIClicked").val($(this).attr("id"));
$("#Button1").click();//this will fire the hidden button event on server side.
});
});
And you'll have which 'li' was clicked in the hiddenfield.
On backend, handle the button click event like this.
protected void Button1_Click(object sender, EventArgs e)
{
if (hdnLIClicked.Value == "basic")
{
//handle your logic for basic here...
}
else if (hdnLIClicked.Value == "new")
{
//handle your logic for new here...
}
}
Hide the button through jQuery, don't set its property Visible="false", or the button won't get rendered on client side.
Enjoy ;)...

Related

asp.net web forms using variables inside a server control

I am trying to use a variable inside server control in asp.net webform page(.aspx). I am getting syntax error. What may be the issue?
<%string msgCancelProject = "You are not authorized to cancel the project."; %>
<asp:Button ID="CancelProject" <%if(IsAuthorized){%> title="<% =msgCancelProject %>" clickDisabled="disable" <%}%> runat="server" Text="Cancel Project" 
                     OnClick="btnCancelProject_Click" 
                    OnClientClick="return confirm('Are you certain you want to cancel the record?');" />
It's not possible to do what you are trying to do with a server control. I.e. adding a property dynamically in markup. You can only set property values but that's not what you want.
You could achieve what you want from the code behind as follows.
Keep your markup like this.
<asp:Button ID="CancelProject" runat="server" Text="Cancel Project" OnClick="btnCancelProject_Click"
OnClientClick="return confirm('Are you certain you want to cancel the record?');" />
And, in your code behind do this.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string msgCancelProject = "You are not authorized to cancel the project.";
if (IsAuthorized)
{
CancelProject.Attributes.Add("title", msgCancelProject);
CancelProject.Attributes.Add("clickDisabled", "disable"); // I'm not sure what you are trying to do here
}
else
{
CancelProject.Attributes.Remove("title");
CancelProject.Attributes.Remove("clickDisabled");
}
}
}
Hope this helps.

ASP.Net WebForms LinkButton click, open new tab, passing id through hidden html input variable

I have an aspx webforms page with a repeater built through a user control. Each repeater item has a link button. What I want to happen is that when the LinkButton (in the repeater on page A's user control) is clicked, the url is opened in a new tab, and a hidden id next to that LinkButton is passed (according web development best practices for security if possible) to the aspx page (page B) in the new tab. Both pages A and page B are in the same application.The intent of what I described above is so that the user can easily return to their search results after returning from the URL opened by clicking on the LinkButton.I am open to ideas on how to do this that are closer to standard best-practice methods.
So far, I have tried:
1) cross-page posting – this worked for passing the id, but not for opening in a new tab.
2) Setting the PostBackUrl to page B's url, setting the Page.Form.Target="_blank" with OnClientClick calling javascript to set the hidden id from the user control to the value of an html hidden input on page B and also.
3) I also tried using window.open("page B url", "_newtab") in OnClientClick.
a) So far, the only method that worked correctly was the 2nd one from the 3 different methods above. However, after page B is loaded in the new tab, I don't know how to reset page A's Page.Form.Target back to what it was previously before setting it to "_blank"
b) The methods that I have tried, to no avail, to reset the Page.Form.Target have been:
1) Resetting the target in page A's Page_Load where IsPostBack == true --> that caused Page B to load with the same content as Page A.
2) Resetting the target in page A's user control's Page_Load --> same result as method 1
3) Resetting the target in page A’s user control’s LinkButton’s OnUnLoad in page A's user control --> same result as method 1
4) Resetting the target in javascript through the LinkButton’s OnClientClick --> didn’t work
5) Resetting the target in page B's Page_Load using a public variable from page A containing a reference to page A's form (similar to what can be done through cross-page posting) --> didn’t work.
What I am thinking about trying next is:
1) Wrapping another user control on page A to display page B's content, in an asp Panel (Panel B)
2) Put page B’s content into the new user control page
3) Wrapping the search results content on page A in an asp Panel (Panel A).
4) When the LinkButton in the repeater on the new user control is clicked, the search results content in Panel A will be hidden, and Panel B will be shown.
5) When the user wants to return to the search results, they will click on a ‘Return to Search’ LinkButton in Panel B’s content, and then Panel B will be hidden, then content of Panel B will be cleared, and Panel A will be shown again.
I'm not yet sure if that will work though. It doesn't seem like this should be that difficult. It is a straight-forward concept, and I would think is a fairly common situation in web development.
I feel like Wiley Coyote trying to catch the Road Runner because I come up with elaborate intelligent, thought-out plans that all completely fail. I am now holding up a little sign that says, "Help!
I had the same issue resolve by the following code you just try this in ur HTML page for a button in GRIDVIEW:
<asp:LinkButton ID="LinkButton1" runat="server" Text="View" CommandArgument='<%# Bind("ref") %>'
OnClick="LinkButton1_Click" OnClientClick="document.forms[0].target ='_blank';">View</asp:LinkButton>***
I actually got this figured out.
I figured it out through a combination of the marked-answer on this post, How to Open new tab when we click on LinkButton, and the marked-answer on this post, Is it possible add click event to hyperlink?.
My Repeater ItemTemplate in the user control's repeater looks similar to this:
<asp:HiddenField ID="hfId" runat="server"
Value='<%# Eval("Id") %>'/>
<asp:HyperLink ID="myLink" runat="server"
Text='<%# Eval("Name") %>'
NavigateUrl="/myUrl.aspx"
Target="_blank" />
<asp:Button ID="btnSubmit" runat="server"
Text="Submit"
OnClick="BtnClick"
Style="display: none;" />
This is my code in the ItemDataBound of the repeater:
protected void RptrItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
var myId = "";
var myNameLink = e.Item.FindControl("myLink") as HyperLink;
if (myNameLink != null)
{
var submitButton = e.Item.FindControl("btnSubmit") as Button;
if (submitButton != null)
{
var submitButtonClientId = submitButton.ClientID;
myNameLink.Attributes.Add("onclick", "onNameClick('" + submitButtonClientId + "')");
}
}
}
}//end RptrItemDataBound
The javascript code:
<script type="text/javascript">
function nameClick(buttonId)
{
document.getElementById(buttonId).click();
}
</script>
And here is the BtnClick C# code:
protected void BtnClick(object sender, EventArgs e)
{
var btnSelect = sender as Button;
if (btnSelect == null)
{
return;
}
var myListItem = (RepeaterItem)btnSelect.DataItemContainer;
if (myListItem != null)
{
var hfId = myListItem.FindControl("hfId") as HiddenField;
if (hfId != null)
{
var intId = int.Parse(hfId.Value);
Session["selectedId"] = intId;
}//end if (hfId != null)
}//end if (myListItem != null)
}//end btnClick

calling a button click event

Is it possible to call "button click event" in one web form ,from a button in another web form?
what actually i'm trying to do is, i've a link button in second form, when it is clicked, i want the first form to disply and also button in first form to be clicked.
can anyone help me do this?
I assume you are using jQuery and have basic knowledge of it. So do it this way:
<form id="form1" style="display:none">
<Asp:Button id="button1" onclick="alert('clicked')" >button1</Asp:Button>
</form>
<form id="form2">
<Asp:LinkButton url="javascript:void(0);" onclick="call1();">Link button1</Asp:LinkButton>
</form>
<script>
function call1()
{
$("#form1").show();
$("#button1").trigger("click");
}
</script>
Note: The asp markup written by me can give compilation errors, so please resolve those yourself. I put that just to give you the basic idea of how it is handled.
create a Delegate in WebForms (I have used in WinForms)
public delegate void LoginDelegate(string s);
loginAirLineDelegate = new LoginDelegate(DisableToolStripMenuItems);
public void DisableToolStripMenuItems(string s)
{
this.viewToolStripMenuItem.Visible = true;
this.bookingToolStripMenuItem.Visible = true;
this.existingUserToolStripMenuItem.Visible = false;
this.newUserToolStripMenuItem.Visible = false;
this.toolStripStatusUserID.Text = "USerID :- "+s;
this.LoginUserId = s;
}
I have passed the Delaqgte to other Form with Construtor as argumnet.
I can able to fire the delegate from the second form like this
logDelegate(textBoxUserName.Text);

ASP, Listview conditional Alert

I'm currently working with a listview in which I want an htmltablecell to possess the onclick property which is driven by the codebehind rather than a javascript.. However I'm guessing that's pretty much a dream getting it to obey the C# code... Anyways this is what I want it to run:
protected void show_anm(object sender, EventArgs e)
{
Label hiddenc = (Label)listview1.FindControl("hidden");
Alert.Show(hiddenc.Text);
}
and here's the Alert class
public static class Alert
{
public static void Show(string message)
{
string cleanMessage = message.Replace("'", "\\'");
string script = "<script type=\"text/javascript\">alert('" + cleanMessage + "');</script>";
Page page = HttpContext.Current.CurrentHandler as Page;
if (page != null && !page.ClientScript.IsClientScriptBlockRegistered("alert"))
{
page.ClientScript.RegisterClientScriptBlock(typeof(Alert), "alert", script);
}
}
}
The point is creating a listview with a two conditional tablecells, one which appears only when a certain condition is met and the other appears every other time (that's alredy sorted out). Where the one demanding a condition is Clickable, and upon clicking it it'll display an Alertbox with Data from a specific DB cell...
Sorry if my language and the question seemes off, English isn't my native language and I haven't doused myself in Coffe yet.
Any help on the matter would be most appritiated
EDIT1*
<asp:Listview ................
<ItemTemplate>
<tr ......>
<td id=default .....>
<asp:label ........ Text='<%# eval("stuff") %> />
</td>
<td id=conditional onclick=alert()..........>
<asp:label ......... Text='<%# eval("stuff") %> />
</td>
<td id=hidden visible=false ...........>
<asp:label ......... Text='<%#eval("stuff i want in alert") %>' />
.....
<script tyupe="text/javascript">
function alert()
{
var msg = document.getElementById("tried with label id and tablecell id nothing seemingly worked").value;
alert(msg);
}
</script>
I recently made a workaround that shows the data I want to display in the labels tooltip but I'd still prefer the alertbox to work properly as it feels more natural to click something.
Edit2 In case anyone is wondering I used the ItemDataBound event to bind the visibility of cells default and conditional within an if clause to make sure the control exists and the conditions are met.
I am confused as to why you're doing what you're doing. Why do you want the codebehind to handle an onclick event of a htmltablecell when you are pumping out javascript to show an alert anyway?
Why not just handle the whole logic within Javascript?
A postback from a htmltablcell will also require javascript
Set your tablecell to call a javascript function which would obtain the alert text from the hidden value and display that;
function ShowAlert()
{
var message = document.getElementbyId("hidden").value;
alert.show(message);
}

Fetch JS return value in server side page_load event in asp.net

I have an aspx master/content page scenario. The parent page has an IFrame which points to a child.aspx. The child.aspx has a checkbox, On page_load of child.aspx, I want to show/hide the checkbox depending on the following logic:
- if the child.aspx is opened directly, then I have to show the checkbox.
- if the child.aspx is opened in the IFrame, then I have to hide the checkbox.
Basically, I want to check in child.aspx, if it contains a parent window then hide the checkbox control otherwise show it.
I will prefer the show/hide code in codebehind in Page_load event as I have to execute some more logic depending on whether the it is opened from parent window or not.
Till now I did the following:
In child.aspx
<asp:Content ID="Content1" ContentPlaceHolderID="Main" Runat="Server">
<script language="javascript" type="text/javascript">
function DoesParentExists()
{
var bool = (parent.location == window.location)? false : true;
var HClientID ='<%=hfDoesParentExist.ClientID%>';
document.getElementById(HClientID).Value = bool;
}
</script>
<div>
<h2>Content - In IFrame</h2>
<asp:HiddenField runat="server" id="hfDoesParentExist" />
<asp:CheckBox ID="chkValid" runat="server" />
<asp:ImageButton ID="ImageButton_FillW8Online" ImageUrl="~/images/expand.gif"
OnClick="btnVerify_Click" runat="server" style="height: 11px" />
</div>
</asp:Content>
in client.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "DoesParentExists", "DoesParentExists()", true);
if (hfDoesParentExist.Value == "true")
{
chkValid.Visible = false;
}
}
Using RegisterClientScriptBlock, I get error in JS. That the object hfDoesParentExist doesn't exist 'coz the control is not yet created. Right? I tried using RegisterStartupScript but in codebehind I always get null in hidden variable. I don't want to use the on button click or something like it. I need it on page_load event only. How to resolve the issue?
This line:
document.getElementById(HClientID).Value = bool;
Should be: (lower case value)
document.getElementById(HClientID).value = bool;
Also you cannot check the value of a hidden field set by javascript register callback, in the current executing context on the server side.
I would move the logic to the client side to hide or show the checkbox. If the field must indeed be removed from the page you can do that as well with javascript.
function DoesParentExists()
{
var bool = (parent.location == window.location)? false : true;
var cehckboxId ='<%=chkValid.ClientID%>';
if(bool){
document.getElementById(cehckboxId).style.display = 'none';
}
else {
document.getElementById(cehckboxId).style.display = 'block';
}
}
You may want to wrap the checkbox with a div and hide the container also to include the label.
To do it server-side, I would rely on a querystring parameter. Have the parent page load the child page by appending ?inframe=1. Then check for that value in your Page_Load.

Categories