|
I found the problem. Case closed!
|
|
|
|
|
Dear colleges,
I have run into the following situation:
webpage1.aspx is using UC1.ascx.
UC1.ascx has a button which as a result will redirect to webPage2.aspx
webPage2.aspx uses UC2.ascx. Within UC2.ascx I have a button (Send).
It seems like pressing the button leads to re-launching webPage2 (it's Page_Load method is being called with IsPostBack set to false).
The problem is that re-launching the page will result in a brand new UC2 instance, which will wipe out all information previously stored in it prior to selecting the Send button.
This is the code in webPage2.aspx.cs:
<br />
namespace conversion<br />
{<br />
public partial class SocketConnectionListPage : System.Web.UI.Page<br />
{<br />
protected void Page_Load(object sender, EventArgs e)<br />
{<br />
if (!IsPostBack)<br />
{<br />
string[] parameters = new string[2];<br />
parameters[0] = Request.Params["protocol"];<br />
parameters[1] = Request.Params["port"];<br />
<br />
SocketConnectionList myAscx = (SocketConnectionList)FindControl("SocketConnectionList1");<br />
myAscx.SetSocketProperties(parameters);<br />
}<br />
}<br />
}<br />
}<br />
|
|
|
|
|
Not sure what the cause of the problem is but I've always tried to avoid doing navigation in user controls. What you could do it have your page subscribe to an event in the user control and when the button is pressed on the user control raise the event and the event handler in the page can do the redirection.
|
|
|
|
|
surround the button 'Send' with an update panel
foreach(Minute m in MyLife)
myExperience++;
|
|
|
|
|
What does that means?
Can you please explain what it does and where I do it?
|
|
|
|
|
I have seen the problem even more weird:
I have a webPage1.aspx which has a registered UC1.ascx.
When you click a button on this web page (all is actually UC1 which is loaded by webPage1.aspx), a new UC1.ascx instance is created. It will happen every click, and has nothing to do with browsing from one page to another.
Any clue?
|
|
|
|
|
I got confused,
Have you two different pages with two different usercontrols
Or it is just one page with user control that is added at runtime
Please, clarify
Or send a sample code
foreach(Minute m in MyLife)
myExperience++;
|
|
|
|
|
Ignore what was previously written to avoid confusion.
I am doing a very simple thing: my aspx page is using a user control. Whenever I click a button, it seems like a new user control class instance is created, and hence all settings stored in the first user control instance were obsolete.
|
|
|
|
|
Following is my aspx file:
<br />
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebServer.aspx.cs" Inherits="conversion.WebServer" %><br />
<br />
<%@ Register src="SocketConnection.ascx" tagname="SocketConnection" tagprefix="uc1" %><br />
<br />
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><br />
<br />
<html xmlns="http://www.w3.org/1999/xhtml" ><br />
<head runat="server"><br />
<title>Socket Connection Test Page</title><br />
</head><br />
<body><br />
<form id="form1" runat="server"><br />
<div><br />
<br />
<uc1:SocketConnection ID="SocketConnection1" runat="server" /><br />
<br />
</div><br />
</form><br />
</body><br />
</html><br />
Following is my ascx file:
<br />
<br />
using System;<br />
using System.Collections;<br />
using System.Configuration;<br />
using System.Data;<br />
using System.Linq;<br />
using System.Web;<br />
using System.Web.Security;<br />
using System.Web.UI;<br />
using System.Web.UI.HtmlControls;<br />
using System.Web.UI.WebControls;<br />
using System.Web.UI.WebControls.WebParts;<br />
using System.Xml.Linq;<br />
using System.Net;<br />
using System.Net.Sockets;<br />
using System.Collections.Generic;<br />
<br />
namespace conversion<br />
{<br />
public partial class SocketConnection : System.Web.UI.UserControl<br />
{<br />
DataTable myDataTable = null;<br />
protected void Page_Load(object sender, EventArgs e)<br />
{<br />
if (!IsPostBack)<br />
{<br />
myDataTable = new DataTable();<br />
}<br />
}<br />
<br />
public void Apply_Click(object sender, EventArgs e)<br />
{<br />
try<br />
{<br />
Response.Redirect("SocketConnectionListPage.aspx?protocol=" + ProtocolDropDownList.Text +<br />
"&port=" + PortTextBox.Text +<br />
"&keepalive=" + KeepAliveRadioButtonList.SelectedValue +<br />
"&s_timeout=" + SendTimeoutTextBox.Text +<br />
"&r_timeout=" + ReceiveTimeoutTextBox.Text +<br />
"&s_b_size=" + SendBufferSizeTextBox.Text +<br />
"&r_b_size=" + ReceiveBufferSizeTextBox.Text +<br />
"&nagle=" + NagleRadioButtonList.SelectedValue +<br />
"&ttl=" + TtlTextBox.Text +<br />
"&dontfrag=" + DontFragmentButtonList.SelectedValue +<br />
"&broadcast=" + BroadcastButtonList.SelectedValue +<br />
"&multicast=" + MulticastRadioButtonList.SelectedValue);<br />
<br />
}<br />
catch (SocketException se)<br />
{<br />
System.Diagnostics.Debugger.Log(0, "1", se.Message);<br />
}<br />
}<br />
<br />
<br />
protected void ProtocolDropDownList_SelectedIndexChanged(object sender, EventArgs e)<br />
{<br />
if (ProtocolDropDownList.Text.Equals("TCP"))<br />
{<br />
KeepAliveRadioButtonList.Enabled = true;<br />
NagleRadioButtonList.Enabled = true;<br />
MulticastRadioButtonList.Enabled = false;<br />
BroadcastButtonList.Enabled = false;<br />
DontFragmentButtonList.Enabled = false;<br />
TtlTextBox.Enabled = false;<br />
}<br />
else<br />
{<br />
KeepAliveRadioButtonList.Enabled = false;<br />
NagleRadioButtonList.Enabled = false;<br />
MulticastRadioButtonList.Enabled = true;<br />
BroadcastButtonList.Enabled = true;<br />
DontFragmentButtonList.Enabled = true;<br />
TtlTextBox.Enabled = true;<br />
}<br />
}<br />
<br />
protected void Reset_Click(object sender, EventArgs e)<br />
{<br />
KeepAliveRadioButtonList.Enabled = true;<br />
NagleRadioButtonList.Enabled = true;<br />
MulticastRadioButtonList.Enabled = false;<br />
BroadcastButtonList.Enabled = false;<br />
DontFragmentButtonList.Enabled = false;<br />
TtlTextBox.Enabled = false;<br />
<br />
KeepAliveRadioButtonList.SelectedValue = "false";<br />
NagleRadioButtonList.SelectedValue = "true";<br />
<br />
ProtocolDropDownList.SelectedValue = "TCP";<br />
PortTextBox.Text = "";<br />
ReceiveBufferSizeTextBox.Text = "";<br />
ReceiveTimeoutTextBox.Text = "";<br />
SendBufferSizeTextBox.Text = "";<br />
SendTimeoutTextBox.Text = "";<br />
}<br />
}<br />
}<br />
Now, in the ascx I put myDataTable variable for stub purposes. I use it to determine if a new SocketConnection user control is created.
It seems like the first time the page is loaded, I see a SocketConnection instance being created.
Then, if you click whatever button in that page, you see another instance of SocketConnection being created (as myDataTable is null).
I need to keep some information, like socket connection objects, in the user control class to be able to show a list of clients currently connected to this server and the messages exchange.
Thanks for your help.
|
|
|
|
|
I might not be fully understanding your problem but....
When the page posts back myDataTable will always be null, it doesn't mean a new instance is being created per se, page level variables aren't persisted between posts,you would need to store it somewhere (i.e. session) and repopulate on post back.
What is the value of PostBack in the user control when you click a button?
|
|
|
|
|
PostBack is false when a button is clicked.
How do I store information in session?
I mean I need to store socket connection objects etc? Any sample?
|
|
|
|
|
I got it.
No new instances are created. Actually the user control is reloaded from the server in each post back.
The solution is to cash the values you want to save between postbacks
There are several ways to do so
ViewState object
Session object
Application object
Cash object
Static Members
But take care, because you have to select the choice suitable to your situation
You will need to read about "State Management" in any ASP.NET refernce
Or try this
Web Aplication State[^]
foreach(Minute m in MyLife)
myExperience++;
|
|
|
|
|
Hi
I have a field which has a carriage return in it. because of this it wont search this field properly and will not display any matching results.
How do you deal with carriage return fields so that I can query them?
thanks
|
|
|
|
|
Filter out those carriage return (\r).
You can either go with a regular expression or make your own string replace function.
e.g. string filteredstring = rawstring.Replace("\r", string.Empty);
Hope this helps.
Niladri Biswas
|
|
|
|
|
hi, i exported the datatable into word table. now what i want to do is that while exporting the datatable i want to restrict user to edit the first column only in word document . is it possible to do it with vb.net code. if yes please help me , thanks in advance.
|
|
|
|
|
trimandir prajapati wrote: is it possible to do it with vb.net code. if yes please help
And what does it has to do with ASP.Net?
Manas Bhardwaj
Please remember to rate helpful or unhelpful answers, it lets us and people reading the forums know if our answers are any good.
|
|
|
|
|
Hi
I am using inline sql to search a table column. Some of the fields in the column have ' ' in them and whenever these fields are chosen it shows an error 'In correct Syntax near the text that is in the ' '.
Is this a problem because it affects the sql syntax in the statement?
My code goes something like 'select from table where field is LIKE '%" + name + "%'"
Any help would be great.
thanks
Jamie
|
|
|
|
|
Could you post a bit of your code? This may help maybe...
Regards
Sebastian
|
|
|
|
|
Try
'select from table where field is LIKE '%" + name.Replace("'", "''") + "%'"
For a better, and safer, solution look into using parameters in your SQL.
|
|
|
|
|
Ive replaced the apostrophies and it doesnt show an error message but now it just wont find that field which is weird. The sql statement is searching a table for fields that match the description the user puts in.
Is this because the apostrophies are not recognised in the field?
thanks
OK forget what I said above.
I think the problem is that there are breaks in the text in the database field, i.e. a new line so this might be causing the error of not finding it.
Is this a known problem when searching database fields that if there is a line break in the field it affects finding data?
thanks
modified on Monday, June 29, 2009 8:26 AM
|
|
|
|
|
Oh I see, the value of name can contain ''...
DoctorMick already gave the answer. But just one advice: Pay attention to sql injection (http://unixwiz.net/techtips/sql-injection.html[^]). You could use preparedstatements within your code. This will be the better (and more secure) way.
Regards
Sebastian
|
|
|
|
|
Try this
select from table where field is LIKE RTRIM(LTRIM('%" + name + "%'))"
Hope this helps
Niladri Biswas
|
|
|
|
|
Hi...
I want to send mail through attachment class.
I want to pass stream data into it,from database with datatype image.
Here is the code....
protected void btnSendMail_OnClick(object sender, ImageClickEventArgs e)
{
MailAddress from = new MailAddress(Config.MailFrom);
MailAddress to = new MailAddress(lblToMail.Text);
MailMessage mail = new MailMessage(from, to);
mail.Subject = lblSubject.Text;
mail.Body = txtMailBody.Text;
mail.IsBodyHtml = true;
mail.Sender = from;
string strCommand = "select ID,FileName,Extension,Iszip,Content1,Image_Type,ByloginCode,OnDate from ZipFile where ID=" + iAttachmentFileCode + " ";
DataTable dt = new DataTable();
if (objDbTrans.ExecuteDataAdapter(strCommand, ref dt))
{
foreach (DataRow row1 in dt.Rows)
{
byte[] reportFile = (byte[])row1["Content1"];
string strfileName = row1["FileName"].ToString();
MemoryStream stmContent;
stmContent = new MemoryStream(reportFile);
Attachment attachFile = new Attachment(stmContent,strfileName);
mail.Attachments.Add(attachFile);
}
}
SmtpClient SmtpMail = new SmtpClient(Config.MailServerSmtp);
SmtpMail.Send(mail);
}
mail is sent with file name,nut its content is blank.
Here Content1 is the datatable field with datatype image, containg binary data.
Plz,help to solve this issue.
Thanks.
Regardds,
PUJA FALDU
|
|
|
|
|
pujafaldu wrote: MemoryStream stmContent;
stmContent = new MemoryStream(reportFile);
using(MemoryStream memStream = new MemoryStream(firstString.Length))
{
memStream.Write(reportFile, 0 , reportFile.Length);
Attachment attachFile = new Attachment(stmContent,strfileName);
mail.Attachments.Add(attachFile);
}
Manas Bhardwaj
Please remember to rate helpful or unhelpful answers, it lets us and people reading the forums know if our answers are any good.
|
|
|
|
|
Hi...
i have made changes according to you.but it not works...
here is the changed code.
byte[] reportFile = (byte[])row1["Content1"];
string strfileName = row1["FileName"].ToString();
MemoryStream stmContent;
stmContent = new MemoryStream(reportFile);
using (MemoryStream memStream = new MemoryStream(reportFile.Length))
{
// Write the first string to the stream.
memStream.Write(reportFile, 0, reportFile.Length);
Attachment attachFile = new Attachment(stmContent, strfileName);
mail.Attachments.Add(attachFile);
}
|
|
|
|