Passing data between asp pages and Message Queuing
-
Languages – ASP.NET and C# I’m reading contents from Microsoft Message Queue (MSMQ) and displaying them in a dynamically generated table over the web page. The table is as show below Message Name Message ID ----------------------------------------------------------- Test Message 1 dde6b7aa-77e6-46be-bd98-f4e67e7e53ec\8204 Test Message 2 dde6b7aa-77e6-46be-bd98-f4e67e7e53ec\8205 Test Message 3 dde6b7aa-77e6-46be-bd98-f4e67e7e53ec\8206 Test Message 4 dde6b7aa-77e6-46be-bd98-f4e67e7e53ec\8207 Test Message 5 dde6b7aa-77e6-46be-bd98-f4e67e7e53ec\8208 Now I want to create hyperlinks to messages in the queue i.e. messages in the first column of the table. The requirement is that when a user clicks on the hyperlink i.e. “Test Message (Number)” he should be able to see details like userid, time, etc. about that particular message in a new webform and at the bottom of that new webform should be a button control to delete the message from the queue. There are two .aspx files. Webform1.aspx - It has all the code to read from the MSMQ, create table and link to webform2. Webform2.aspx – It should have details of just ONE particular message which was clicked by the user in WebFrom1. I’m able to create table and generate link from weform1 to webform2 for the particular message. But I’m not able to proceed from there. How do I Pass the message name from webform1 to webform2, so that webform2 can retrieve the details about the particular message and delete it. Code for webform1 is as below ------------------------------- public class WebForm1 : System.Web.UI.Page { protected System.Web.UI.WebControls.Table Table1; private void Page_Load(object sender, System.EventArgs e) { // Put user code to initialize the page here int rowCount = 0; MessageQueue sthQ = new MessageQueue(".\\Private$\\Sth"); MessageEnumerator sthEnum = sthQ.GetMessageEnumerator(); sthEnum.Reset(); while(sthEnum.MoveNext()) { TableGen(rowCount, sthEnum.Current.Label, sthEnum.Current.Id); rowCount++; } } private void TableGen(int rowCount, string msgLabel, string msgId) { for (int j= (rowCount); j<(rowCount+1); j++) { TableRow r = new TableRow(); TableCell c = new TableCell(); c.Controls.Add(new LiteralControl("" + msgLabel)); r.Cells.Add(c); TableCell c2 = new TableCell(); c2.Controls.Add(new LiteralControl(msgId)); r.Cells.Add(c2); Table
-
Languages – ASP.NET and C# I’m reading contents from Microsoft Message Queue (MSMQ) and displaying them in a dynamically generated table over the web page. The table is as show below Message Name Message ID ----------------------------------------------------------- Test Message 1 dde6b7aa-77e6-46be-bd98-f4e67e7e53ec\8204 Test Message 2 dde6b7aa-77e6-46be-bd98-f4e67e7e53ec\8205 Test Message 3 dde6b7aa-77e6-46be-bd98-f4e67e7e53ec\8206 Test Message 4 dde6b7aa-77e6-46be-bd98-f4e67e7e53ec\8207 Test Message 5 dde6b7aa-77e6-46be-bd98-f4e67e7e53ec\8208 Now I want to create hyperlinks to messages in the queue i.e. messages in the first column of the table. The requirement is that when a user clicks on the hyperlink i.e. “Test Message (Number)” he should be able to see details like userid, time, etc. about that particular message in a new webform and at the bottom of that new webform should be a button control to delete the message from the queue. There are two .aspx files. Webform1.aspx - It has all the code to read from the MSMQ, create table and link to webform2. Webform2.aspx – It should have details of just ONE particular message which was clicked by the user in WebFrom1. I’m able to create table and generate link from weform1 to webform2 for the particular message. But I’m not able to proceed from there. How do I Pass the message name from webform1 to webform2, so that webform2 can retrieve the details about the particular message and delete it. Code for webform1 is as below ------------------------------- public class WebForm1 : System.Web.UI.Page { protected System.Web.UI.WebControls.Table Table1; private void Page_Load(object sender, System.EventArgs e) { // Put user code to initialize the page here int rowCount = 0; MessageQueue sthQ = new MessageQueue(".\\Private$\\Sth"); MessageEnumerator sthEnum = sthQ.GetMessageEnumerator(); sthEnum.Reset(); while(sthEnum.MoveNext()) { TableGen(rowCount, sthEnum.Current.Label, sthEnum.Current.Id); rowCount++; } } private void TableGen(int rowCount, string msgLabel, string msgId) { for (int j= (rowCount); j<(rowCount+1); j++) { TableRow r = new TableRow(); TableCell c = new TableCell(); c.Controls.Add(new LiteralControl("" + msgLabel)); r.Cells.Add(c); TableCell c2 = new TableCell(); c2.Controls.Add(new LiteralControl(msgId)); r.Cells.Add(c2); Table
If you are passing data between two pages, typically it is done by either using the
Session
object as intermediate storage, or by using a querystring. If neither of those is to your liking, you can create a public property on the first form and access it through theHttpContext.Handler
object from the second form. The caveat here is that in order to use this mehtod, you have to be moving from page to page usingServer.Transfer
. Assuming that I have two pages, WebForm1 and WebForm2, the code for WebForm2 would look something like:private void Page_Load(object sender, System.EventArgs ea)
{
WebForm1 firstForm = HttpContext.Current.Handler as WebForm1;
string messageId = null;
if (firstForm != null)
{
messageId = firstForm.MessageId;
}
}
However, I have to ask... are you sure that using two pages is the right way to go? In my opinion, you'd be better off turning WebForm2 into a user control. WebForm1 could then host WebForm2 and manipulate its methods/properties to retrieve and display the message details. Far less needless jumping around. Hope that helps. :) --Jesse
-
Languages – ASP.NET and C# I’m reading contents from Microsoft Message Queue (MSMQ) and displaying them in a dynamically generated table over the web page. The table is as show below Message Name Message ID ----------------------------------------------------------- Test Message 1 dde6b7aa-77e6-46be-bd98-f4e67e7e53ec\8204 Test Message 2 dde6b7aa-77e6-46be-bd98-f4e67e7e53ec\8205 Test Message 3 dde6b7aa-77e6-46be-bd98-f4e67e7e53ec\8206 Test Message 4 dde6b7aa-77e6-46be-bd98-f4e67e7e53ec\8207 Test Message 5 dde6b7aa-77e6-46be-bd98-f4e67e7e53ec\8208 Now I want to create hyperlinks to messages in the queue i.e. messages in the first column of the table. The requirement is that when a user clicks on the hyperlink i.e. “Test Message (Number)” he should be able to see details like userid, time, etc. about that particular message in a new webform and at the bottom of that new webform should be a button control to delete the message from the queue. There are two .aspx files. Webform1.aspx - It has all the code to read from the MSMQ, create table and link to webform2. Webform2.aspx – It should have details of just ONE particular message which was clicked by the user in WebFrom1. I’m able to create table and generate link from weform1 to webform2 for the particular message. But I’m not able to proceed from there. How do I Pass the message name from webform1 to webform2, so that webform2 can retrieve the details about the particular message and delete it. Code for webform1 is as below ------------------------------- public class WebForm1 : System.Web.UI.Page { protected System.Web.UI.WebControls.Table Table1; private void Page_Load(object sender, System.EventArgs e) { // Put user code to initialize the page here int rowCount = 0; MessageQueue sthQ = new MessageQueue(".\\Private$\\Sth"); MessageEnumerator sthEnum = sthQ.GetMessageEnumerator(); sthEnum.Reset(); while(sthEnum.MoveNext()) { TableGen(rowCount, sthEnum.Current.Label, sthEnum.Current.Id); rowCount++; } } private void TableGen(int rowCount, string msgLabel, string msgId) { for (int j= (rowCount); j<(rowCount+1); j++) { TableRow r = new TableRow(); TableCell c = new TableCell(); c.Controls.Add(new LiteralControl("" + msgLabel)); r.Cells.Add(c); TableCell c2 = new TableCell(); c2.Controls.Add(new LiteralControl(msgId)); r.Cells.Add(c2); Table
Jesee gave you an excellent reply. However, I will say that my preferred method is to pass the information using the QueryString. In other words, set up your links to the second page like this: Webform2.aspx?messageID=123 Then inside Webform2 you can retrieve the value by using
Request["messageID"]
. Regards, Alvaro