I have several linkbuttons in my master page. I need to add css class "Active" after I click each linkbutton and postback URLs.
<asp:LinkButton ID="Linkbutton1" runat="server" PostBackUrl="/News.aspx?lang=1"
Text="News" OnClick="Linkbutton1_Click">
</asp:LinkButton>
Linkbutton 1
Linkbutton 2 - class "active"
Linkbutton 3
I tried to add class using linkbutton onclick event, but after postback css class has been removed.
Put this to the Linkbutton1_Click method:
Linkbutton1.CssClass = "active";
Article on MSDN.
if you want write in code behind you can work with cookies:
in Linkbutton1_Click method:
Response.Cookies["Linkbutton1-cssClass"].Value = "active";
in Page_Load method:
if(Request.Cookies["Linkbutton1-cssClass"] != null)
Linkbutton1.CssClass = Server.HtmlEncode(Request.Cookies["Linkbutton1-cssClass"].Value);
finaly you can use foreach for all LinkButtons
onclick of linkbutton call javascript function changestyle(SenderID)
<script type="javascript">
var strPreviousCahnge=""
function changestyle(SenderID)
{
var LinkButtonActive=document.getelementbyid(senderID);
LinkButtonActive.className="subTabActive";
if (strPreviousCahnge!= "" && strPreviousCahnge!= id)
{
var identity=document.getElementById(strPreviousCahnge);
identity.className="subTabInactive";
}
strPreviousCahnge=SenderID
}
</script>
Related
I have a grid view that looks generally like this
<asp:GridView ID="MyGridView"
OnSelectedIndexChanged="OtherAddressGrid_SelectedIndexChanged"
OnRowDataBound="OnRowDataBoundOther"
<Columns></Columns>
</asp:GridView>
and on OnSelectedIndexChanged I want to change the postback url to add &TAB=something so that I can change the page to the selected tab using the Request.QueryString["TAB"]. To do this I set the onclick to a postback in OnRowDataBoundOther. This is what I have so far
protected void OnRowDataBoundOther(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes["onclick"] = Page.ClientScript.GetPostBackClientHyperlink(MyGridView, "Select$" + e.Row.RowIndex);
e.Row.Attributes["style"] = "cursor:pointer";
}
}
question
How do I add custom parameters to this postback.
You can add your own select button to the Gridview that triggers the OnSelectedIndexChanged. You only have to add CommandName="select" to it in order to work. You also need to add a PostBackUrl to the button and a custom property that holds the value of TAB (in this snippet it is the row index)
<asp:Button ID="Button1" runat="server" Text="Select" CommandName="select"
data-item='<%# Container.DataItemIndex %>' PostBackUrl="~/Default.aspx?TAB=" />
You now have a button that looks something like this in HTML.
<input type="submit" name="GridView1$ctl05$Button1" value="Button"
onclick="javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions("GridView1$ctl05$Button1", "", false, "", "Default.aspx?TAB=", false, false))"
id="mainContentPane_GridView1_Button1_3" data-item="3" />
As you can see aspnet added an onclick function to the button because you added a PostBackUrl. That function contains the url that will be used for PostBack. All we have to do now is change that url by adding the custom property with jQuery.
<script type="text/javascript">
$(document).ready(function () {
$('#<%=GridView1.ClientID %> input[type=submit]').each(function () {
var item = $(this).attr('data-item');
$(this).attr("onclick", $(this).attr("onclick").replace("?TAB=", "?TAB=" + item));
});
});
</script>
enter code here NET application, I have inserted a button that call a Javascript function (OnClick event) and a asp.net function (OnClick event)
The problem is that when I click the button, it refreshes the page.
How can I avoid the page refreshes click on asp button using javascript?
document.getElementById('pageurl').innerHTML = "tryfblike.aspx";
$('#<%= btnsave.ClientID %>').click();
$('#auth-loggedout').hide();
<asp:Button runat="server" ID="btnsave" OnClick="btnsave_Click();" Visible="true" style="display: none;" />
protected void btnsave_Click(object sender, EventArgs e)
{
objda.agentid = "2";
string currenttime = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");
objda.datetime = currenttime;
ds = objda.tbl_log();
}
First to call JavaScript function, use OnClientClick.
To avoid page refresh, after your function call add return false;
For example:
<asp:Button ID="btnSubmit" runat="Server" OnClientClick="btnsave_Click(); return false;" />
in java script you can use return false.
if you want prevent whole page referesh use ajax.
FirstYou can call javascrip onclick and then simply add
return false;
I have the following ItemTemplate in my GridView:
<ItemTemplate>
<asp:Button UseSubmitBehavior="false" runat="server" ID="btnShow" CssClass="btnSearch" Text="View All" CommandName="ViewAll" OnClientClick="myfunction(); return false;" OnCommand="btnShow_Command" CommandArgument='<%#((GridViewRow)Container).RowIndex%>' />
</ItemTemplate>
For the ItemTemplate I have a button which opens a popup window when clicked by using the following JQuery:
$(document).ready(function () {
$(".btnSearch").click(function (e) {
e.preventDefault();
//centering with css
centerPopup();
//load popup
loadPopup();
});
});
function myfunction() {
}
my Command code-behind:
protected void btnShow_Command(object sender, CommandEventArgs e)
{
int index = 0;
if (e.CommandName == "ViewAll")
{
index = Convert.ToInt32(e.CommandArgument);
DataTable cacheTable = HttpContext.Current.Cache["ResultsTable"] as DataTable;
string column = cacheTable.Rows[index].Field<string>("Guideline");
string test = BookingResults.Rows[index].Cells[7].Text;
string html = HttpUtility.HtmlDecode(column);
ResultsDiv.InnerHtml = html;
//tbGL.Text = html;
//upData.Update();
//MessageBox.Show(index.ToString());
}
}
I added the OnClientClick="myfunction(); return false;" because it was doing a postback each time I clicked. If I have multiple rows, it only works the first time I click but any time after, the popup is not displayed when another or the same button is clicked.
How do I resolve it so no matter which button is clicked the popup is displayed without doing a postback?
Actually you have not showed up the implementation of your method myfunction(), in case the myfunction() method have any syntactical error then the OnClientClick event will be void and it will post-back/submit the form to the server.
Try to remove the call from OnClientClick and just implement your logic at jquery on click event by using class selector as follows
$(document).ready(function () {
$(".btnSearch").click(function (e) {
e.preventDefault();
alert($(this).val() + " Clicked"); // you can put your method mymethod() here
// you can put youe popup logic here
return false;
});
});
You can also see this example of js fiddle
Put it out on the tag or <%: Html.BeginForm %> tag
OnClientClick="return myfunction();
function myfunction(){
// you can put youe popup logic here
return false;
}
Using like this your button never do post back.
How do I submit the id from one page to another page in ASP.NET using C#? I have a GridView control and each record contains a link button:
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="lbtn_naatscat" runat="server" Text="arshad" CommandName="view_user_naats_gv" Font-Underline="false" />
</ItemTemplate>
</asp:TemplateField>
I've bound the id to link button:
if (e.Row.RowType == DataControlRowType.DataRow)
{
((LinkButton)e.Row.FindControl("lbtn_naatscat")).CommandArgument
= DataBinder.Eval(e.Row.DataItem, "cate_id").ToString();
((LinkButton)e.Row.FindControl("lbtn_naatscat")).Text
= DataBinder.Eval(e.Row.DataItem, "title").ToString();
}
now I want to pass this id to another page when user click to this link button.
You can handle the LinkButton's Command event and use the CommandArgument:
void LinkButton_Command(Object sender, CommandEventArgs e)
{
if(e.CommandName == "view_user_naats_gv")
{
Resonse.Redirect("UserNaats.aspx?catID=" + e.CommandArgument.ToString());
}
}
You can create a Session for this purpose.
Do as follows:
Session["Id"]=e.CommandArgument.ToString() //Id you want to pass to next page
In this way your Session variable will get created. And you will be able to access it on the next page.
While retrieving it on next page:
Id=Session["Id"]
Other Alternatives:
View state
Control state
Hidden fields
Cookies
Query strings
Application state
Session state
Profile Properties
State management Techniques in ASP.NET:
http://msdn.microsoft.com/en-us/library/75x4ha6s%28v=vs.100%29.aspx
Hope its helpful.
Store it in a hidden field, and access it on the next page in the code behind file.
Here is another SO post with
Another example of passing hidden field in ASP
Best of luck!
I would use a HyperLinkField field like so
<asp:HyperLinkField DataNavigateUrlFields="Database ID Field"
DataNavigateUrlFormatString="~/PageName.aspx?ID={0}"
DataTextField="Database ID Field" HeaderText="ID" />
And then on the page that you're redirecting to, you would check the query string for ID
string id = Request.QueryString["ID"]
Set the PostBackUrl and CommandArgument properties of the LinkButton in your source page like so:
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="lbtn_naatscat" runat="server" Text="arshad" CommandName="view_user_naats_gv" Font-Underline="false" PostBackUrl="~/YourPage.aspx" CommandArgument='<%# Eval("submitid")%>' />
</ItemTemplate>
</asp:TemplateField>
Then on your page that is being submitted to you can place the following directive to the DestinationPage.aspx page:
<%# PreviousPageType VirtualPath="~/SourcePage.aspx" %>
I don't know what type of container the <ItemTemplate> tag is from (ListView?). However you can find the control in the DestinationPage.aspx page like so:
if (Page.PreviousPage != null && Page.PreviousPage.IsCrossPagePostback) {
// if you use the PreviousPageDirective you should be able to access the ListView
// property directly
// var listView = Page.PreviousPage.MyListView;
var listView = Page.PreviousPage.FindControl("MyListView") as ListView;
var linkButton = listView.FindControl("lbtn_naatscat") as LinkButton;
var submitId = linkButton.CommandArgument;
}
Using Row_Command event you can send it next page using query string.
protected void gvDeals_RowCommand(object sender, GridViewCommandEventArgs e)
{
if(e.CommandName.Equals("MyCommand")
{
string value=calculate();
Response.Redirect("~/MyPage.aspx?item=" + value);
}
}
I think it would make more sense to use a hyperlink in your GridView.
For example:
<ItemTemplate>
<asp:HyperLink ID="link_naatscat" runat="Server"
Text="arshad"
ToolTip="arshad"
NavigateUrl=<%#"~/YourPage.aspx?id=" + Eval("submitid")%>>
</asp:HyperLink>
</ItemTemplate>
Send the id this way
Response.Redirect("/yourpage.aspx?" + id);
And get it this way
String s = "";
foreach (String item in Request.QueryString.Keys)
{
s = Request.QueryString[item];
}
if (s != "")
{
id = Int32.Parse(s);
}
I have a repeater which displays and data bind the source of tab links. Here is the code:
protected void rptTab_ItemBound(Object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
Item i = e.Item.DataItem as Item;
Link hlTabLink = e.Item.FindControl("hlTabLink") as Link;
hlTabLink.Target = Sitecore.Links.LinkManager.GetItemUrl(i);
hlTabLink.DataSource = i.Paths.FullPath;
hlTabLink.Field = "Title";
}
}
Now this is my markup:
<asp:Repeater ID="rptTab" runat="server" OnItemDataBound="rptTab_ItemBound">
<ItemTemplate>
<li id= "liTabTest" runat = "server">
<a>
<sc:Link ID = "hlTabLink" Field = "scTabLink" onclick = "TabClick()" runat ="server"></sc:Link>
</a>
</li>
</ItemTemplate>
</asp:Repeater>
And this is the jQuery which is adding CSS class dynamically based on which item is selected:
$(document).ready(function () {
init();
});
function init() {
$("ul#Tab-labels li").removeClass("tab-label TabbedPanelsTabSelected");
$("ul#Tab-labels li:first").addClass("tab-label TabbedPanelsTabSelected");
};
function TabClick() {
alert('test');
};
Now you can see that I am displaying the CSS file based on the index of the tab. I also have to add the click event in that jQuery. So there are basically two things that I am confused:
My click event in jQuery is not getting called :(
From jQuery I have to know in the click event that which page it has
to go through? So that I have to get from codebehind or what? Like a
hidden field and store the tab pages in that, then fetch out from
jQuery.
How should I resolve this? Please help!
For sc:Link, change the bind method to onClientClick for onclick = "TabClick()". OnClick is for the postback server method.
BTW, what is the sc prefix? Is there a user control you are adding to the page?