Please look at 1st image.
In this page i use LinkButton. ( Like... "sr001","sr003", and all )
.aspx page
<asp:LinkButton ID="lnkbtn" runat="server" onclick="lnkbtn_Click"
ValidationGroup='<%# Eval("pid") %>'><%#Eval("productcode") %></asp:LinkButton>
.cs page
protected void lnkbtn_Click(object sender, EventArgs e)
{
int id;
id = Convert.ToInt32(((LinkButton)sender).ValidationGroup.ToString());
string abc = "http://development.in/prod-more-info.aspx?pid=" + id;
Response.Write("<script>window.open('" + abc.ToString() + "','_blank');</script>");
}
Now, When I clik on this link button process work successfully.
BUT..............
My design is disturb by this process, look at 2nd image..
How can I fix this problem?
PLEASE HELP ME....
Doing a raw Response.Write is not advisable since you are literally just appending content to the response stream.
If you want to ensure your script shows up in the proper place and executes you should use ClientScriptManager.RegisterStartupScript instead.
ClientScriptManager cs = Page.ClientScript;
if (!cs.IsStartupScriptRegistered(this.GetType(), "MoreInfoPopup"))
{
int id;
id = Convert.ToInt32(((LinkButton)sender).ValidationGroup.ToString());
string abc = "http://development.in/prod-more-info.aspx?pid=" + id;
string script = String.Format("<script>window.open('{0}','_blank');</script>",abc);
cs.RegisterStartupScript(this.GetType(), "MoreInfoPopup", script);
}
Are you just wanting to reload the page once the button is clicked? If so, do a response redirect to Request.Url.AbsolutePath.
The code:
Response.Redirect(System.Web.HttpContext.Current.Request.Url.AbsolutePath);
Related
I'm doing a C# 3-tier project about pets
On load of the first webform I make a query to the database and get the data of all pets (id, name, photo) as a List to be shown as cards with images in HTML, I'm using Materialize by the way, I leave this link as example http://materializecss.com/cards.html
My code behind (Pets.aspx.cs) is this
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
NegPet negPet = new NegPet();
StringBuilder sbHtml = new StringBuilder();
List<EntPet> listEntPet = negPet.getAllPet(5); //5 is the user code (get all pets from user 5)
if (listEntPet.Count > 0)
{
foreach (EntPet entPet in listEntPet)
{
sbHtml.Append("<div class=\"col s6 m4\">");
sbHtml.Append("<div class=\"card\">");
sbHtml.Append("<div class=\"card-image\">");
sbHtml.Append("<img src=\"http://www.dogzone.com/images/breeds/beagle.jpg\" alt=\"\" class=\"circle responsive-img\" />");
sbHtml.Append("<asp:LinkButton id=\"lb_"+ entPet.Id_pet + "\" runat=\"server\" CommandArgument='<%# Eval(\""+ entPet.Id_pet + "\") %>')");
sbHtml.Append("\" click=\"updatePet_Click\" class=\"btn-floating halfway-fab waves-effect waves-light red\"><i class=\"material-icons\">edit</i></a>");
sbHtml.Append("</div>");
sbHtml.Append("<div class=\"card-content\">");
sbHtml.Append("<span class=\"card-title\">");
sbHtml.Append(entPet.Name_pet);
sbHtml.Append("</span>");
sbHtml.Append("</div>");
sbHtml.Append("</div>");
sbHtml.Append("</div>");
}
} else
{
sbHtml.Append("<h2>No pets found</h2>");
}
galPet.Controls.Add(new Literal { Text = sbHtml.ToString() });
}
}
Where galPet is a
<asp:PlaceHolder ID="galPet" runat="server" />
This code returns me all the "Id" and "Name" of "Pets" and sets it in the HTML design that I want, similar to a gallery. The problem comes when I try to get to the event onClick="updatePet_Click" apparently it never reaches it's method behind
public void updatePet_Click(Object sender, EventArgs e)
{
LinkButton btn = (LinkButton)(sender);
string yourValue = btn.CommandArgument.Substring(3);
ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('" + yourValue + "');", true);
}
What I'm trying to do with this code is retrieve the ID from the clicked asp:LinkButton so I can use it in code behind
I also tried changing the event to "OnClick" but I got this error when tried to click it
Pets.aspx:24 Uncaught ReferenceError: updatePet_Click is not defined
at HTMLUnknownElement.onclick (Pet.aspx:24)
I would like to know how to retrieve the ID to work it in the code behind or if there is another way to pass my list to the HTML design where I can get the clicked ID easier. Thanks
I have written a project in C# in which I load a HTML webpage if an event occurs during teh course of the project usage.
My issue is that inside my html page, I have a href node inside a tag as such:
<a href="http://pseudo01.hddn.com/vod/demo.flowplayervod/flowplayer-700.flv"
style="display:block;width:inherit;height:inherit;background-color: black;overflow:hidden"
id="player">
</a>
I would like to change the href value programatically by sending a C# variable (called myHrefFile) depending on what the user did.
How can this be done?
you can do something like this
<a href="<%= someMethodThatGetHrefValue() %>"
style="display:block; width:inherit;height:inherit; background-color:black;
overflow:hidden"
id="player">click</a>
call the method that decide what should be href for link .
and put all the logic of deciding href in that method like this
public string someMethodThatGetHrefValue()
{
if(someval == true)
return "http://www.google.com";
else
return "http://www.yahoo.com";
}
Something like this perhaps? This is an Asp.net solution.
<asp:LinkButton ID="LinkBut" runat="server" onclick="LinkBut_Click">Click here</asp:LinkButton></p>
protected void LinkBut_Click(object sender, eventArgs e)
{
string myHrefFile = "";
if(Value.equals(true))
{
myHrefFile = "page1.aspx";
}
else
{
myHrefFile = "page2.aspx";
}
Response.Redirect(myHrefFile );
}
I'am developing with c# and asp.net.
I have some pages with update panels. In this update panels there is a button that redirects to a new window. This is done by calling the window.open with the scriptmanager in code behind. Everything works fine until I don't use https. If I use tunnelling with a router to have a secure line till the router and then use a proxy to access my webpages, the window.open called from the buttons that are in an update panel open a new register card, but there I get the error : page not found. In the url I can see that the router did not put the proxy+IP before the path. That does not happen if I remove the update panel.With Firebug I could see that with the update panel I get a POST and in the resonse the contentType is text/plain. Without the update panel there is a GET and the response contentType is text/html. So what can I do to run this without removing the update panels?
This works fine until I don't use https over my proxy:
protected void btnPrint_Click(object sender, EventArgs e)
{
url = "~/Gui/Report/ReportViewer.aspx?ReportName=CustomerReport";
Page page = (Page)HttpContext.Current.Handler;
if (page == null) {
Redirect(url);
}
url = page.ResolveUrl(url);
string script = #"window.open(""{0}"");";
script = String.Format(script, url);
ScriptManager.RegisterStartupScript(page,
typeof(Page),
"Redirect",
script,
true);
}
<asp:UpdatePanel ID="UpdatePanel" runat="server">
<ContentTemplate>
<dx:ASPxButton runat="server" ID="btnPrint"
Text="print" OnClick="btnPrint_Click">
</dx:ASPxButton>
</ContentTemplate>
</asp:UpdatePanel>
Got it....
the information that the scriptlanguage is javascript is lost if I am using the proxy (strange...?!?!).
If I build the script tags on my own with adding the attribut "language='javascript'" then everything works fine.
Can anybody tell me why this information is lost?
Here the new code:
protected void btnPrint_Click(object sender, EventArgs e)
{
url = "~/Gui/Report/ReportViewer.aspx?ReportName=CustomerReport";
Page page = (Page)HttpContext.Current.Handler;
url = page.ResolveUrl(url);
string script = "window.open('" + url + "');";
System.Text.StringBuilder sb = new System.Text.StringBuilder();
sb.Append("<script language='javascript'>");
sb.Append(script);
sb.Append("</script>");
ScriptManager.RegisterStartupScript(page,
typeof(Page),
"Redirect",
script,
false);
}
I have one image button in the custom control like below.
public string SearchTableName = string.Empty;
public string SearchColumnName = string.Empty;
public string SiteURL = string.Empty;
ImageButton _imgbtn;
protected override void OnInit(EventArgs e)
{
_imgbtn = new ImageButton();
_imgbtn.ImageUrl = ImageURL;
_imgbtn.OnClientClick = "ShowSearchBox('" + SiteURL +"/_layouts/CustomSearch/SearchPage/Searchpage.aspx?table_name=" + SearchTableName + " &column_name=" + SearchColumnName + "')";
}
On Clicking of the image button I want to migrate to the another window which is a popup. For this I written a javascript function. I am setting the SearchTableName and SearchColumnName in the web page in which we are consuming this custom control like below. Before consuming I registered this control in web page with register tag.
<ncc:SearchControl runat="server" ID="txtSearchControl" /> In code behind file of this webpage I am using following code to set the values.
protected void Page_Load(object sender, EventArgs e)
{
txtSearchControl.ImageURL = "_layouts/Images/settingsicon.gif";
txtSearchControl.SearchTableName = "Employees";
txtSearchControl.SearchColumnName = "LastName";
txtSearchControl.SiteURL = "http://Sp2010:8787";
}
Now coming to the problem, when I click the image button the SearchTableName and SearchColumnName values are not coming. I think I am calling OnClientClick function, thats why the values are not being set. But how to set the values for the custom control based on the values setting in the webpage. If I use the Click function will it serve my purpose? If so, how to call that javascript function from this click event.
Finally got solution. I am initializing the values in the page init method in the custom control. Thats why the values i am setting in the visual webpart page are not being captured. Now I changed the initializing the values in CreateChildControl method. Now it works perfectly. Thank you.
I'm setting up a Web User Control in ASP 4. The control itself works correctly, and the code for the onload even is the same as used in my standard aspx pages.
protected void GetTranslationImage(object sender, EventArgs e)
{
ImageButton image = (ImageButton)sender;
objTranslation = new TranslationsHelper();
string sTranslationID = image.ImageUrl.ToString();
string lang = ((NRMaster)this.Master).Language;
lang = lang == null ? "en-gb" : lang;
image.ImageUrl = objTranslation.GetTranslation(sTranslationID, lang);
}
When the object is passed through onLoad it doesn't contain the default URL during GetTranslationImage. The object changes to the translation required however it reverts back following in the HTML on the page load.
The definition of the ImageButton is as follows:
<asp:ImageButton ID="btnSearch" runat="server" name="Search" value="Search" class="Search" src="/_resources/img/BTN_search.gif" onclick="btnSearch_Click" onLoad="GetTranslationImage" />
Does the control alter the time this function needs to be run.
Any help would be greatly appreciated.
Thank You
James
Why are you setting src="/_resources/img/BTN_search.gif" if you are overwriting it in OnLoad?
1: Remove it
Or
2: Change it to:
ImageUrl="/_resources/img/BTN_search.gif"
What if you made the transition method public from within the usercontrol and you simply call out to that method in your page load?
page_load()
{
myusercontrol.StartImageTransition();
}
Public Methods inside User control c# .net