Thursday, March 29, 2012
Poping up a window AND redirecting the page thtat triggered the po
I have a page where the user inserts some stuff and when he clicks on the
submit button a popup window showing that info is launched and the page "A"
where he was is redirected to another page, page "B". This page "B" can also
be accessed by other means. Can this be done? If so, how?
I was trying to do this:
protected void button_Click(object sender, EventArgs e)
{
string popupScript = String.Format("<script language='javascript'>" +
"window.open('ShowReport.ashx','CustomPopUp'," +
"'width=600, height=500, menubar=no, resizable=yes,
toolbar=no,
location=no, statusbar=no, left=212, top=184')</script>");
Page.RegisterStartupScript("ShowReport", popupScript);
Page.Response.Redirect("NewPage.aspx");
}
But what happens is that I get imediatly redirected to the NewPage.aspx. If
I'm not wrong, this happens because the current page isn't reloaded after
this event is treated. What I am asking is, for the effect I want (both the
pop-up and the redirect occurs) what can I do? I have also thought on trying
to open the pop-up on the loading of NewPage.aspx, by passing some specific
value when I make the redirect (something like
Response.Redirect("NewPage.aspx?popup=yes").
Any ideas/sugestions?
Thanks in advanceHi Ricardo,
The popup window must be generated on the client. This means that the page
must be loaded to generate the popup. What you can do is to have the page
pop up a window, and then submit back to the server, where the Redirect can
occur.
HTH,
Kevin Spencer
Microsoft MVP
.Net Developer
You can lead a fish to a bicycle,
but it takes a very long time,
and the bicycle has to *want* to change.
"Ricardo Videira" <RicardoVideira@.discussions.microsoft.com> wrote in
message news:2ABEC8F1-C366-4934-BE57-01641826EA8F@.microsoft.com...
> Hi to all. I'm having trouble with the following situation:
> I have a page where the user inserts some stuff and when he clicks on the
> submit button a popup window showing that info is launched and the page
> "A"
> where he was is redirected to another page, page "B". This page "B" can
> also
> be accessed by other means. Can this be done? If so, how?
> I was trying to do this:
> protected void button_Click(object sender, EventArgs e)
> {
> string popupScript = String.Format("<script
> language='javascript'>" +
> "window.open('ShowReport.ashx','CustomPopUp'," +
> "'width=600, height=500, menubar=no, resizable=yes,
> toolbar=no,
> location=no, statusbar=no, left=212, top=184')</script>");
> Page.RegisterStartupScript("ShowReport", popupScript);
> Page.Response.Redirect("NewPage.aspx");
> }
>
> But what happens is that I get imediatly redirected to the NewPage.aspx.
> If
> I'm not wrong, this happens because the current page isn't reloaded after
> this event is treated. What I am asking is, for the effect I want (both
> the
> pop-up and the redirect occurs) what can I do? I have also thought on
> trying
> to open the pop-up on the loading of NewPage.aspx, by passing some
> specific
> value when I make the redirect (something like
> Response.Redirect("NewPage.aspx?popup=yes").
> Any ideas/sugestions?
> Thanks in advance
>
if a page has a redirect header (produced by calling Redirect), the browser
will not render the html, if you want the html rendered, you need to use a
meta tag with a refresh. also popup blocks will prevent you popup window
anyway. you should change the button to a html hyperlink that opens the
report.
-- bruce (sqlwork.com)
"Ricardo Videira" <RicardoVideira@.discussions.microsoft.com> wrote in
message news:2ABEC8F1-C366-4934-BE57-01641826EA8F@.microsoft.com...
> Hi to all. I'm having trouble with the following situation:
> I have a page where the user inserts some stuff and when he clicks on the
> submit button a popup window showing that info is launched and the page
> "A"
> where he was is redirected to another page, page "B". This page "B" can
> also
> be accessed by other means. Can this be done? If so, how?
> I was trying to do this:
> protected void button_Click(object sender, EventArgs e)
> {
> string popupScript = String.Format("<script
> language='javascript'>" +
> "window.open('ShowReport.ashx','CustomPopUp'," +
> "'width=600, height=500, menubar=no, resizable=yes,
> toolbar=no,
> location=no, statusbar=no, left=212, top=184')</script>");
> Page.RegisterStartupScript("ShowReport", popupScript);
> Page.Response.Redirect("NewPage.aspx");
> }
>
> But what happens is that I get imediatly redirected to the NewPage.aspx.
> If
> I'm not wrong, this happens because the current page isn't reloaded after
> this event is treated. What I am asking is, for the effect I want (both
> the
> pop-up and the redirect occurs) what can I do? I have also thought on
> trying
> to open the pop-up on the loading of NewPage.aspx, by passing some
> specific
> value when I make the redirect (something like
> Response.Redirect("NewPage.aspx?popup=yes").
> Any ideas/sugestions?
> Thanks in advance
>
Saturday, March 24, 2012
Populate insert form field from GridView selection
Hi,
I'm trying to do something that seems like it should be simple, but I'm having trouble getting it to work. I have a page with a GridView and a FormView in Insert mode.
When the user selects a row in the gridview, I want to take the data keys from that selection and populate them to the appropriate textboxes in the form. I'm trying to do this in the"GridView1_SelectedIndexChanged event" with the following code:
TextBox t;t = (TextBox)FormView1.FindControl("myTextBox");
t.Text = (String)GridView1.SelectedDataKey.Values[0];The text in "myTextBox" doesn't change, it remains blank. I've tried to find another event in which to put this code but so far no luck. I'm a newbie so I'm sure it's something obvious.
Can anyone help me with this? Thanks in advance.
Try moving your code to the FormView.DataBound event.
Hi Ed,
When I do that, I get "Object reference not set to an instance of an object. " I'm guessing that this is because the formview1_databound fires when the page is first loading, before there is a selected key from the gridview. I'm not sure that this is the case, it's just my best guess.
Do you have any suggestion for a workaround or alternate approach?
Thanks.
Once try this
ProtectedSub GridView1_SelectedIndexChanged(ByVal senderAsObject,ByVal eAs System.EventArgs)Handles GridView1.SelectedIndexChangedFormview1.PageIndex = GridView1.SelectedIndex
EndSub
Hi Mahesh,
Okay, now I have this code but I still don't see the text I want populated into the text box. What am I doing wrong?
protectedvoid GridView1_SelectedIndexChanged(object sender,EventArgs e){
FormView1.PageIndex = GridView1.SelectedIndex;
TextBox t;t = (TextBox)FormView1.FindControl("myTextBox");
t.Text = (String)GridView1.SelectedDataKey.Values[0];
}
How about this:
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
for (int i = 0; i < GridView1.Rows.Count; i++)
{
if (GridView1.SelectedIndex == i)
{
TextBox t;
t = (TextBox)FormView1.FindControl("myTextBox");
t.Text = (String)GridView1.SelectedDataKey.Values[0];
}
}
}
Within your FormView.DataBound event, simply place a conditional statement which checks for the existence of a SelectedItem.
if (!GridView1.SelectedIndex.Equals(-1))
That did it! Thanks very much (I told you that I was a newbie :-).
Friday, March 16, 2012
Populating a Dropdownlist from a SQL table
Here is what i got so far. Im not sure what goes after the read? Thanks you very much.
Sub GetCauseofLoss()
Dim connAs SqlClient.SqlConnection
Dim cmdAs SqlClient.SqlCommand
Dim drAs SqlClient.SqlDataReader
Dim intFieldAsInteger
conn =New SqlClient.SqlConnection
conn.ConnectionString = Replace(Application("ConnectStage"), "Driver={SQL Server}; ", "")
cmd = conn.CreateCommand
conn.Open()
cmd.CommandText = "SELECT * FROM CauseOfLoss"
dr = cmd.ExecuteReader
While dr.Read
dgddLossCode.SelectedValue &= vbNewLine
For intField = 0To dr.FieldCount - 1
?????
Next
EndWhile
dr.Close()
conn.Close()
EndSub
' Assume myDDL is an established web controlDim myDS as DataSet
' Populate DataSet however you want.
Dim currRecord as DataRow
myDDL.Items.Add(new ListItem("", "blank")) ' Just addin
for each currRecord in myDS.Tables(0).Rows
myDDL.Items.add(new ListItem(currRecord(0)))
next
The index in parenthesis after Tables can be replaced with a string of a table name you proved.
The index in parenthesis after currRecord can be replaced with a string of a column name you proved or the name of the column from the database.
I haven't used DataReader before, thus my example above with a DataSet. Looking at the documentation for it, you would do something similar (and in this case, probably easier).
While dr.Read()
dgddLossCode.Items.Add(new ListItem(dr.Item(COLUMN_INDEX_OR_NAME_HERE)) ' assuming that is you DropDownList
End While
The above example is assuming you only want 1 column. If you want multiple columns from the same row, you can name each column individually, or you can iterate through them or use a method of the class. You can find those athttp://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemdatasqlclientsqldatareadermemberstopic.asp
If you do that, use a String variable and then add the String to the DropDownList
check this
The table Cause of Loss has a Loss _Code, and a Loss_Description. I would need to populate the ddlist with both columns. I also didnt mention that this ddlist is part of a datagrid. Thank you very much guys for your help. Does this look OK?
Sub GetCauseofLoss()
Dim conn As SqlClient.SqlConnection
Dim cmd As SqlClient.SqlCommand
Dim myDS as DataSet
Dim currRecord as DataRow
Dim dr As SqlClient.SqlDataReader
Dim intField As Integer
conn = New SqlClient.SqlConnection
conn.ConnectionString = Replace(Application("ConnectStage"), "Driver={SQL Server}; ", "")
cmd = conn.CreateCommand
conn.Open()
cmd.CommandText = "SELECT * FROM CauseOfLoss"
dr = cmd.ExecuteReader
While dr.Read
for each currRecord in myDS.Tables(0).Rows
dgddLossCode.Items.Add(new ListItem(dr.Item(Loss_Code))
dgddLossCode.Items.Add(new ListItem(dr.Item(Loss_Description))
next
End While
dr.Close()
conn.Close()
End Sub
Each add call on the dropdownlist.item will add a new list item. What you need to do is similar, but more like
Dim strToAdd as String
While dr.Read
strToAdd = ""
for each currRecord in myDS.Tables(0).Rows
strToAdd &= dr.Item(Loss_Code)
strToAdd &= dr.Item(Loss_Description)
dgddLossCode.Items.Add(strToAdd)
next
End While
You may want to space them somehow, but I'm not sure the best method on that. As for the ddl being in a DataGrid, I'm not sure that would change this part of the code. It's still DropDownList, just encapsulated within a DataGrid.
vin1127 wrote:
Sub GetCauseofLoss()
Dim conn As SqlClient.SqlConnection
Dim cmd As SqlClient.SqlCommand
Dim myDS as DataSet
Dim currRecord as DataRowDim dr As SqlClient.SqlDataReader
Dim intField As Integer
conn = New SqlClient.SqlConnection
conn.ConnectionString = Replace(Application("ConnectStage"), "Driver={SQL Server}; ", "")
cmd = conn.CreateCommand
conn.Open()cmd.CommandText = "SELECT * FROM CauseOfLoss"
dr = cmd.ExecuteReader
While dr.Readfor each currRecord in myDS.Tables(0).Rows
dgddLossCode.Items.Add(new ListItem(dr.Item(Loss_Code))
dgddLossCode.Items.Add(new ListItem(dr.Item(Loss_Description))
next
Hi, your DataSet was not filled and it is empty and uninstantiated, your foreach loop would not run. Try this
protected void dgrd_ItemDataBound(Object sender, DataGridItemEventArgs e)
{
// if in <EditItemTemplate>, use ListItemType.EditItem
if(e.Item.ItemType == ListItemType.Item ||
e.Item.ItemType == ListItemType.AlternatingItem)
{
// open connection here
// DataSet declaration
// retrieve the fields needed only
string strSQL = "SELECT Loss_Code, Loss_Description FROM CauseOfLoss";
SqlDataAdapter daCOL = new SqlDataAdapter(strSQL,conn);
daCOL.Fill(myDS,"COL");
DataColumn dcol = new DataColumn();
dcol.ColumnName = "CompositeCol";
dcol.ColumnType = System.Type.GetType("System.String");
dcol.Expression = "Loss_Code + ' - ' + Loss_Description";
myDS.Tables["COL"].Columns.Add(dcol);
// locate DDL in DataGrid
DropDownList ddl = (DropDownList)e.Item.FindControl("ddlIDInGrid");
ddl.DataSource = myDS.Tables["COL"];
ddl.DataTextField = "CompositeCol";
ddl.DataValueField = "Loss_Code";
ddl.DataBind();
}
}
Hope this helps...
vin1127 wrote:
The table Cause of Loss has a Loss _Code, and a Loss_Description. I would need to populate the ddlist with both columns. I also didnt mention that this ddlist is part of a datagrid. Thank you very much guys for your help. Does this look OK?
Sub GetCauseofLoss()
Dim conn As SqlClient.SqlConnection
Dim cmd As SqlClient.SqlCommand
Dim myDS as DataSet
Dim currRecord as DataRowDim dr As SqlClient.SqlDataReader
Dim intField As Integer
conn = New SqlClient.SqlConnection
conn.ConnectionString = Replace(Application("ConnectStage"), "Driver={SQL Server}; ", "")
cmd = conn.CreateCommand
conn.Open()cmd.CommandText = "SELECT * FROM CauseOfLoss"
dr = cmd.ExecuteReader
While dr.Readfor each currRecord in myDS.Tables(0).Rows
dgddLossCode.Items.Add(new ListItem(dr.Item(Loss_Code))
dgddLossCode.Items.Add(new ListItem(dr.Item(Loss_Description))
nextEnd While
dr.Close()
conn.Close()End Sub
Your code should be this.
Sub GetCauseofLoss()
Dim conn As SqlClient.SqlConnection
Dim cmd As SqlClient.SqlCommand
Dim myDS as DataSet
Dim currRecord as DataRow
Dim dr As SqlClient.SqlDataReader
Dim intField As Integer
conn = New SqlClient.SqlConnection
conn.ConnectionString = Replace(Application("ConnectStage"), "Driver={SQL Server}; ", "")
cmd = conn.CreateCommand
conn.Open()
cmd.CommandText = "SELECT * FROM CauseOfLoss"
dr = cmd.ExecuteReader
While dr.Read
for each currRecord in myDS.Tables(0).Rows
dgddLossCode.Items.Add(new ListItem(dr.Item("Loss_Code"),dr.Item("Loss_Description"))
next
End While
dr.Close()
conn.Close()
End Sub
for each currRecord in myDS.Tables(0).Rows
dgddLossCode.Items.Add(new ListItem(dr.Item("Loss_Code"),dr.Item("Loss_Description"))
next
The above will actually add a new ListItem to your DDL with a Text of the value in Loss_Code and a Value of "Loss_Description". If this is what you want, thent that is correct. If you want them to both be added, you'll need to make them 1 string, then add that string. On a note, if you only supply on parameter to ListItem, it is made both the Text and Value property.