DotNetNuke : Pass a data from one module to another module - asp.net

In am developing a website by using DotNetNuke, module by module.
In a page (tab), I have 3 modules. 2 modules are the same module which are Form modules, however I name it for different name (Section A and Section B).
In my Button modules it involve processing of Section A and Section B how can I pass data from Form Modules with Section A and Section B into Button modules within same page (Tabs)?

For that you need the IModuleCommunicator and the IModuleListener interfaces.
On the module that will send data:
public partial class View : Module1, IModuleCommunicator
{
public event ModuleCommunicationEventHandler ModuleCommunication;
protected void Page_Load(object sender, EventArgs e)
{
try
{
sendDataToOtherModule("This is a test.");
}
catch (Exception ex)
{
Exceptions.ProcessModuleLoadException(this, ex);
}
}
public void sendDataToOtherModule(string valueToSend)
{
ModuleCommunicationEventArgs mcea = new ModuleCommunicationEventArgs();
mcea.Target = "TheOtherModule";
mcea.Value = valueToSend;
ModuleCommunication(this, mcea);
}
}
On the module that will receive the data
But you could use this code in every module and check the Target.
public partial class View : Module2, IModuleListener
{
protected void Page_Load(object sender, EventArgs e)
{
try
{
//module code
}
catch (Exception ex)
{
Exceptions.ProcessModuleLoadException(this, ex);
}
}
public void OnModuleCommunication(object sender, ModuleCommunicationEventArgs e)
{
if (e.Target == "TheOtherModule")
{
Label1.Text = e.Value.ToString();
}
}
}
Add using DotNetNuke.Entities.Modules.Communications on both modules.

Related

What is the point of HttpApplication.CompleteRequest()?

I have made a simple web form with some static HTML on the design view, and on the code behind I put on two methods: Page_Load and Page_PreRender as follows:
public partial class SamplePage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Response.ClearContent();
Response.ClearHeaders();
Response.StatusCode = (int)HttpStatusCode.NotFound;
Respons.Write("Not Found");
Context.ApplicationInstance.CompleteRequest();
}
protected void Page_PreRender(object sender, EventArgs e) // Why does this event get called? Should not the CompleteReqeust()
{ // cause the page to jump directly to the end of events pipeline?
throw new NotImplementedException();
}
}
Moreover, I have read so many Q&As about Response.End() being "ugly", "dangerous", etc, even from MSDN website. But it puzzles me a lot, why if so the Response.Redirect(string) still uses Response.End() internally?
Overriding IHttpHandler's ProcessRequest(HttpContext) in the Page is enough to do the trick.
public partial class SamplePage : System.Web.UI.Page
{
public override void ProcessRequest(System.Web.HttpContext context)
{
if (conditionTrue)
{
context.Response.StatusCode = 404;
context.ApplicationInstance.CompleteRequest();
}
else
{
base.ProcessRequest(context);
}
}
protected void Page_Load(object sender, EventArgs e) // This is not called also :P
{
}
protected void Page_PreRender(object sender, EventArgs e) // Not called now :)
{
throw new NotImplementedException();
}
}

display next page asynchronously and then run its page load event

I have an airline site in which i am trying to display the fare from different API/Web service. But Firstly i want to display the search page--> Display processing --> binding data in the page from theses API/web service as they received.
but i am not able to display search page before the result processing.
What i have tried (code)-
public partial class asyncgridview : System.Web.UI.Page,ICallbackEventHandler
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsCallback)
{
ltCallback.Text = ClientScript.GetCallbackEventReference(this, "'bindgrid'", "EndGetData", "'asyncgrid'", false);
}
}
private string _Callback;
public string GetCallbackResult()
{
return _Callback;
}
public void RaiseCallbackEvent(string eventArgument)
{
DataTable table = fetchData();
gvAsync.DataSource = table;
gvAsync.DataBind();
using (System.IO.StringWriter sw = new System.IO.StringWriter())
{
gvAsync.RenderControl(new HtmlTextWriter(sw));
_Callback = sw.ToString();
}
}
}
regards
Avishek

ASP.Net Dynamically switch Master Pages

Never needed to do this before but is it possible to dynamically set/change which master page a page is using? Have an old asp.net web forms project which I have created a new bootstrap template for but the boss wants to give people the opportunity to switch on the new one instead of forcing it upon them.
I would recommend you to create a BasePage class than write this method in that class and inherit all of your pages from this class whose master page can be changed dynamically.
public class BasePage: System.Web.UI.Page
{
protected void Page_PreInit(object sender, EventArgs e)
{
try
{
if (conduction1)
this.Page.MasterPageFile = "~/MasterPage.master";
else
this.Page.MasterPageFile = "~/Master.master";
}
catch (Exception ex)
{
}
}
}
And then in your page inherit page from BasePage like this
public partial class _Default:BasePage
The master page is changed only in preint event
protected void Page_PreInit(object sender, EventArgs e)
{
try
{
if (conduction1)
this.Page.MasterPageFile = "~/MasterPage.master";
else
this.Page.MasterPageFile = "~/Master.master";
}
catch (Exception ex)
{
}
}
or
void page_PreInit(object sender, EventArgs e)
{
Page page = sender as Page;
page.MasterPageFile = "string location of masterpage";
}

How to retain Serial Port State after postback ASP.net

I am trying to send the data to serial port in ASP.net. After connecting to serial port Before postback data is being sent. But after postback i get exception while sending data.
'System.InvalidOperationException: The port is closed.'
I tried everything by connecting to port on pageload: ispostback, and disconnecting and connecting again. Still it shows same exception. Is there any way to retain the state of serial port..
here's my code. Please Help me Out...
public partial class _Default : System.Web.UI.Page
{
string indata;
public SerialPort sp = new SerialPort();
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
openPort("COM10");
disconnect();
connect();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
//disconnect();
openPort("COM10");
connect();
check(TextBox1.Text); //Data Sending Successful but after postback even it doesnt work too.
}
public void connect()
{
try { sp.Open(); }
catch (Exception e1) { MessageBox.Show(e1.ToString()); }
}
public void disconnect()
{
try { sp.Close(); }
catch (Exception e1) { MessageBox.Show(e1.ToString()); }
}
public void openPort(string p)
{
sp.BaudRate = 9600;
sp.Parity = Parity.None;
sp.StopBits = StopBits.One;
sp.DataBits = 8;
sp.Handshake = Handshake.None;
sp.PortName = p;
sp.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);
// sp.ReadTimeout = 200;
// sp.WriteTimeout = 200;
}
private void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e)
{
SerialPort sp = (SerialPort)sender;
indata = sp.ReadExisting();
Debug.WriteLine(" Data Received:");
Debug.Write(" " + indata);
}
protected void Button4_Click(object sender, EventArgs e)
{
check("" + (char)26); //Exception in sending
}
protected void Button3_Click(object sender, EventArgs e)
{
check("\r\n"); //exception in sending
}
protected void Button2_Click(object sender, EventArgs e)
{
check(TextBox1.Text); // exception in sending
}
void check(string ss)
{
//sp.Dispose();
//openPort("COM10"); connect();
if (sp.IsOpen)
sp.Write(ss);
else
{
disconnect(); openPort("COM10"); connect();
sp.Write(ss);
}
}
}
I would simplify your code, so the port is configured on page load and the one handler deals with resetting your port. The disconnect, connect, I see is complicating it. Here I have given an example of using the button click event.
Please note the missing brace below.
public partial class _Default : System.Web.UI.Page
{
string indata;
public SerialPort sp = new SerialPort();
protected void Page_Load(object sender, EventArgs e)
{
sp.BaudRate = 9600;
sp.Parity = Parity.None;
sp.StopBits = StopBits.One;
sp.DataBits = 8;
sp.Handshake = Handshake.None;
sp.PortName = p;
sp.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);
// sp.ReadTimeout = 200;
// sp.WriteTimeout = 200;
}
if (!Page.IsPostBack)
{
sp.BaudRate = 9600;
sp.Parity = Parity.None;
sp.StopBits = StopBits.One;
sp.DataBits = 8;
sp.Handshake = Handshake.None;
sp.PortName = p;
sp.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);
// sp.ReadTimeout = 200;
// sp.WriteTimeout = 200;
}
protected void Button1_Click(object sender, EventArgs e)
if sp.IsOpen = False then
{
try { sp.Open(); }
catch (Exception e1) { MessageBox.Show(e1.ToString()); }
}
else
{
try { sp.Close(); }
catch (Exception e1) { MessageBox.Show(e1.ToString()); }
}
void check(string ss)
{
//sp.Dispose();
//openPort("COM10"); connect();
if (sp.IsOpen)
{//missing brace
sp.Write(ss);
}//missing brace
else
{
sp.Open();
sp.Write(ss);
}
}
}
Edit 2:
As I mentioned in the comments the code will only run once.
The following examples are provided from the link below.
Have you tried writing some codes under the !IsPostBack code block to
check if the codes hits there when it postbacks? try this below for
testing
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
Response.Write("First load");
}
else
{
Response.Write("Postback occurs");
}
}
OR
I will refer the code you want to run as One Time Code. For what you
are attempting to achieve, following should work. Please note that
sessions also expire. So after about 20 minutes (default value) of
inactivity, if the user comes back to the site/hits refresh, the One
Time Code will run again. If you want something more persistent than
20 minutes you can try using cookies, but if user clears their cookies
your One Time Code with run again.
protected void Page_Load(object sender, EventArgs e)
{
if (Session["firsttimeuser"] == null)
{
//put code here for One Time Code;
Session["firsttimeuser"] = true;
}
}
Please see this link:
There is lengthy discussion about this.
http://forums.asp.net/t/1314918.aspx/1
You should be able to create a solution from this, please advise.
Edit 1
Please see MSDN for Get Port Names:
Use the GetPortNames method to query the current computer for a list
of valid serial port names. For example, you can use this method to
determine whether COM1 and COM2 are valid serial ports for the current
computer.
http://msdn.microsoft.com/en-us/library/system.io.ports.serialport.getportnames.aspx
And SerialPort.Open
_serialPort.PortName = SetPortName(_serialPort.PortName)
http://msdn.microsoft.com/en-us/library/system.io.ports.serialport.open.aspx
Edit 3
Try:
if (!IsPostBack) or
if(!Page.IsPostBack)
Please see:
Implementation of IsPostBack in page load
What is a postback?
and:
http://msdn.microsoft.com/en-us/library/ms178472.aspx

How to call a page method in the dynamically addedded usercontrol?

I am adding a user control dynamically on a page, the user control has a save button that takes data from both user control and page to save in the DB, in the same save method i want to access a method wrriten in the page, so i that mehod had code to rebind the grid kept in the page.
So how can i call a page method in the dynamically added user control?
I was going to suggest creating a base class for your pages, but found an even better way to accomplish this task:
http://www.codeproject.com/Articles/115008/Calling-Method-in-Parent-Page-from-User-Control
Control code:
public partial class CustomUserCtrl : System.Web.UI.UserControl
{
private System.Delegate _delWithParam;
public Delegate PageMethodWithParamRef
{
set { _delWithParam = value; }
}
private System.Delegate _delNoParam;
public Delegate PageMethodWithNoParamRef
{
set { _delNoParam = value; }
}
protected void Page_Load(object sender, EventArgs e)
{
}
protected void BtnMethodWithParam_Click(object sender, System.EventArgs e)
{
//Parameter to a method is being made ready
object[] obj = new object[1];
obj[0] = "Parameter Value" as object;
_delWithParam.DynamicInvoke(obj);
}
protected void BtnMethowWithoutParam_Click(object sender, System.EventArgs e)
{
//Invoke a method with no parameter
_delNoParam.DynamicInvoke();
}
}
Page code:
public partial class _Default : System.Web.UI.Page
{
delegate void DelMethodWithParam(string strParam);
delegate void DelMethodWithoutParam();
protected void Page_Load(object sender, EventArgs e)
{
DelMethodWithParam delParam = new DelMethodWithParam(MethodWithParam);
//Set method reference to a user control delegate
this.UserCtrl.PageMethodWithParamRef = delParam;
DelMethodWithoutParam delNoParam = new DelMethodWithoutParam(MethodWithNoParam);
//Set method reference to a user control delegate
this.UserCtrl.PageMethodWithNoParamRef = delNoParam;
}
private void MethodWithParam(string strParam)
{
Response.Write(“<br/>It has parameter: ” + strParam);
}
private void MethodWithNoParam()
{
Response.Write(“<br/>It has no parameter.”);
}
}

Resources