For my internal webpage at work, I display a DataGrid based on entries in a SQL table (not directly, but with some processing on entries).
Each row in the DataGrid has a button that the user clicks. I need this button to open a new window or tab (I believe I can't decide as this is based on browser config) and also change a value in the SQL table to say the button was clicked.
If I use an asp:Hyperlink then the page opens nicely, but I don't know how to update SQL. Vice-versa if I use an asp:LinkButton I can get the SQL updated but can't get a new page to open.
Is what I am trying to do impossible?
Thanks
EDIT:
I've tried both these in my .cs file, but neither worked:
ClientScript.RegisterStartupScript(GetType(), "openwindow", "window.open('" + url + "','_preview'");
Response.Write("<script type='text/javascript'>detailedresults=window.open('" + url + "');</script>");
<asp:TemplateField HeaderText="Action"> <ItemTemplate>
<asp:LinkButton ID="lbtnConfigureSurvey" runat="server" CausesValidation="False"
CommandName="ConfigureSurvey" CommandArgument='<%# Eval("intSurveyID") %>'
OnClientClick="aspnetForm.target ='_blank';">
</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
protected void gvSurveyList_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "ConfigureSurvey")
{
Response.Redirect('Your page url');
}
}
You can use LinkButton and use OnClientClick property to set the java-script that would open the page in new browser window. This script must return true so that post-back can happen and you can handle click event on server side to update your database.
IMO, better way would be to use hyper-link and open the page in new browser window. However, you must pass a query-string parameter while opening the page (say Page2) that would tell the new page that it should update the database. So url for opening the page will be something like "page2.aspx?recordId=xyz". And within page2.aspx.cs, you will have code such as
protected void page_load(object sender, EventArgs e)
{
if (!IsPostback)
{
var recordId = Response.QueryString["recordId"];
if (!string.IsNullOrEmpty(recordId))
{
// update database
...
}
}
...
}
From security perspective, you may want to include time-out within parameter value and encrypt the entire thing.
You could use your LinkButton and call RegisterStartupScript on postback to execute something like this on page load
.aspx:
<asp:LinkButton OnClick="OnButtonClick" Text="Update" runat="server" />
.aspx.cs:
protected void OnButtonClick(object sender, EventArgs e)
{
ClientScript.RegisterStartupScript(GetType(), "openwindow", "<script type=text/javascript> window.open('About.aspx'); </script>");
}
You need to supply the whole script element to RegisterStartupScript.
try this
Response.Write("<script type='text/javascript'>detailedresults=window.open('PAGE NAME.aspx');</script>")
EDIT:
you can set command name for link button and execute code in rowcocommand event
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName.Equals("commandname for linkbutton"))
{
//update code
Response.Write("<SCRIPT language=\"javascript\">open('Yourpage.aspx','_blank','top=0,left=0,status=yes,resizable=yes,scrollbars=yes');</script>");
}
}
Hyperlink will not call server side event on click, so you won't get post back to perform the SQL task, and by using the LinkButton you can open the page in the new window or tab. To do so you need to add a little code below in the Grid_RowBoundEvent.
Ex: LinkButton.OnClientClick="window.open('http://www.google.com');";
The url you can change as per your requirement.
Again, in the Code behind you will get a server side event. There you can do your sql task.
Hope it will be helpful to you.
Related
When user will click on button, I want to open one .aspx/.html page in different tab and open one .aspx/.html page in same tab.
Sample code:
string redirect = "<script>window.open('../User/Profile.html');</script>";
Response.Write(redirect);
Response.Redirect("../User/NewUser.aspx",true);
Thanks in Adance!!!
No, the response redirect writes in the http's header the "location" value and can only have one, but you can write a javascript like the next for do what you need:
window.open('../User/Profile.html', 'tabName');
window.location.href = '../User/NewUser.aspx';
Good luck!
We can achieve by using Javascript and code behind page
Call Javascript Window.Open() function on Clientclick property to open in new window.
and onClick call your code behine buttonClick Event to redirect on same window.
ASPX Page:
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" OnClientClick="javascript:window.open('http://google.com','_blank');return true;" />
Code behind On Click function:
protected void Button1_Click(object sender, EventArgs e)
{
Response.Redirect("http://google.com");
}
This code not working on Chrome :
window.location.href = '../User/NewUser.aspx';
But you can use this code instead of window.location.href then its working in all browsers :
setTimeout(function(){document.location.href = "page.html"},500);
So I'm creating a dummy webpage for basics right now. The Goal:
On a button click, I want to update information shown in the textbox.
The method myButton_Click takes in the parameters of the button. How can I access an object like textbox (or any object for that matter) since it is not being accessed from a buttonclick event? I set up a public variable, myTextBox_, which I think I could then edit freely. But I'm still not sure how to set myTextBox_ to understand that it is connected to the webpage.
Html:
<form id="form1" runat="server">
<p style="height: 324px">
<asp:Button ID="myButton" runat="server" EnableTheming="True" Text="Button" onclick="myButton_Click"/>
<asp:TextBox ID="myTextBox" runat="server">Hello</asp:TextBox>
</p>
</form>
Then the C# Code:
Textbox myTextBox_;
protected void Page_Load(object sender, EventArgs e)
{
//possibly initialization code set myTextBox_ to the id myTextbox, but how?
}
protected void myButton_Click(object sender, EventArgs e)
{
myTextbox_.text = "goodbye";
}
You're very close. All you need to do is:
protected void myButton_Click(object sender, EventArgs e)
{
myTextBox.Text = "goodbye";
}
You don't need to set up anything in the Page_Load method, as you have them set up as runatserver, so they are accessible from the code behind.
As already stated, this will require a postback to work, to avoid this use AJAX or do it in pure JavaScript.
If you want to learn web on .NET, you should start learning ASP.NET MVC, not web pages.
Anyhow, I believe you don't get the same behaviour as in a desktop app, because when you click the button, you get a page postback (whole page is refreshed). You should use the page's postback event to change the text, not the button.
Or you should use the old "UpdatePanel" from ASP.NET Ajax 1.0, to put the button and the textbox in the same UpdatePanel (but you must first install that ASP.NET Ajax 1.0)
But pls, start using MVC :)
http://www.asp.net/mvc
I have faced an issue while deploying a web application(asp.net c#, in IIS). What I have is single application for two DBs. So, Through querystring, I am determining the connectionstring in the master page.
My problem,is that I have two links each with separate QSs and when clicked, should lead to the corresponding version of the same application.Which is fine, for now when used one at a time. But when used side by side, they are producing the same DB application, as a replica of the first opened URL (which I guess is based on the server session).
Any suggestion on how to achieve this? Appreciate your help. Thanks!
I'm assuming that the idea at the moment is user will click the link on a page and this will redirect them to another page and on this page create the database connection based on the querystring.
Why not store the link clicked in a session variable, which you can then access on the page you're redirected to and create the DB link from that. If you're using a link button or something like that you could do:
Main Page
ASP.NET:
<asp:LinkButton ID="lnkButton1" Text="Link Button 1" OnClick="lnkMyButton_Clicked" runat="server" />
<asp:LinkButton ID="lnkButton2" Text="Link Button 2" OnClick="lnkMyButton_Clicked" runat="server" />
C#
protected void lnkMyButton_Clicked(object sender, EventArgs e)
{
LinkButton lnkButton = (LinkButton)sender;
Session["LinkID"] = lnkButton.ID;
Response.Redirect("MyNewPage.aspx");
}
On your new page:
protected void Page_Load(object sender, EventArgs e)
{
string LinkID = Session["LinkID"];
if(!String.IsNullorEmpty(LinkID))
{
if(LinkID == "lnkButton1") //create connection if first link clicked
else if(LinkID == "lnkButton2") //create connection if second link is clicked
else //handle error
}
else //handle null or empty string
}
I've not tested this, but let me know if it doesn't work.
I have an asp:GridView control inside a .aspx page that the user can add several rows of data to. The user must also be able to attach a file to each row of data added.
For this I use the following inside the GridView:
<asp:TemplateField HeaderText="Upload" HeaderStyle-Width="120px">
<EditItemTemplate>
<asp:FileUpload ID="fuUploadLocation" runat="server" Width="98%" TabIndex="18" />
</EditItemTemplate>
</asp:TemplateField>
Then to save the location of the file upload I use the RowUpdating event in code-behind to set the value etc.
The problem is I can't register a PostBackTrigger for the control on the html as it doesn't pick it up as it's inside the GridView. I've tried setting it dynamically from other examples but can't seem to get this to work, the result being my FileUpload's FileName is always empty and the file then doesn't save correctly.
Any suggestions would be awesome.
Thanks
On Gridview row databound you have to find the file upload control and then add it to your UpdatePanel postback trigger.
I've not found a solution to my problem but I did work around it (sort of). I know just add a link to the grid, when user clicks it sends ID through via querystring, a new page opens that handles the entire upload process and returns the url of the saved document.
Not ideal but it works and saved me hours of frustration.
In the OnItemDataBound event handler of the grid need to call ScriptManager.GetCurrent(this).RegisterPostBackControl(ControlID);
This is an old post but for anyone that is stuck with it, you need to add the following to your control as stated in the other answers.
protected void ItemDataBound(object sender, EventArgs e)
{
Button myButton = (Button)e.Item.FindControl("myButton");
if (myButton != null)
ScriptManager.GetCurrent(Page).RegisterPostBackControl(myButton);
}
You also need to change the enctype in your form tag to the following e.g.:
protected void Page_PreRender(object sender, EventArgs e)
{
Page.Form.Attributes.Add("enctype", "multipart/form-data");
}
It should then work without an issue.
In an ajaxified RadGrid I want two buttons to cause a postback (bypass Ajax) and execute some back-end code. Here's what I have so far...
Buttons (just showing one for simplicity):
<telerik:GridTemplateColumn HeaderText="Actions">
<ItemTemplate>
<asp:ImageButton ID="btnEdit" runat="server" OnClientClick="realPostBack();" ImageUrl="~/images/icon_edit.png" style="display: inline-block" ToolTip="Edit" CommandName="fbEdit" />
</ItemTemplate>
</telerik:GridTemplateColumn>
Javascript function I'm using to disable the postback:
<script type="text/javascript">
function realPostBack(eventTarget, eventArgument) {
$find("<%= RadAjaxPanel1.ClientID %>").__doPostBack(eventTarget, eventArgument);
}
Code-behind I want to execute:
protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
{
if (e.CommandName == "fbEdit")
{
//grab variables from row's cells
string userID = e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["UserID"];
string userName= e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["UserName"];
string userEmail = e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["UserEmail"];
//DO SOME PROCESSING STUFF
}
if(e.CommandName == "fbDelete")
{
//delete record
}
}
So the post back does indeed occur, but my code never fires. I'm guessing it's because the event is tied to the grid and not to buttons im "un-ajaxifying", but I went this way because I NEED to capture the values of some of the cells in the row that the button was clicked.
Maybe I can rework this to use the buttons onClick Event, but I would still need to capture those values.
Can anyone help?
From the look of things you are attempting to execute code in the OnItemCommand event of the RadGrid, but it seems like you might be making things a bit over-complicated. Why not use a GridButtonColumn, set the ButtonType to ImageButton and then set the same CommandName as you do above? As long as you're subscribing to the OnItemCommand event this should trigger everything to get called and your conditional check for e.CommandName == "fbEdit" should be hit as well.
For an example of how to use this RadGrid specific column (in a similar manner) you can check out this demo.
In you need to add bellow code in PageLoad Event.
if (!IsPostBack)
{
RadGridName.Rebind();
}
thats it.