Adding Java Script to the SharePoint WebPart

There are two ways, you can place a javascript in your webpart.

Scenario –1

Let’s consider, you have a javascript file with some javascript functions. You want to call one of the client side functions inside the webpart code. Here goes the solution

Java script File
----------------------------------------------------------------------
function do_confirmService(ID)
{
var textbox = document.GetElementByID(ID);
alert(textbox.value);
}
------------------------------------------------------------------------------
Code

Place the javascript file in Layouts folder and register it on PreRender function. Code in the webpart file goes here
------------------------------------------------------------------------------------

------------------------------------------------------------------------------------
Complete Path of the Layout folder is %Program Files%/Common Files/web server extensions/12/Template/Layouts

2.Call the javascript function in Client event. Code goes here
----------------------------------------------------------------------------------------
protected override void CreateChildControls()

{
TextBox sample = new TextBox();
sample.ID = "sampletextbox";

Button testButton = new Button();
testButton.Text = "click me";
testButton.Attributes.Add("OnClick", "javascript:doConfirmService(" + this.ClientID + "_sampletextbox");

this.Controls.Add(sample);
this.Controls.Add(testButton);
}
--------------------------------------------------------------------------------------
Now deploy the webpart.Enter some text in the textbox and click the button.It will alert the textbox value you entered.

Scenario –2

Suppose you don’t want to go for the javascript file and want to place the javascript in the code itself ,then here is the solution

///
/// Method Name : OnPreRender
/// Description : This method is for registering JavaScript file
///

/// EventArgs

protected override void OnPreRender(EventArgs e)
{
string script= @" function do_confirmService(ID)
{
var textbox = document.GetElementByID(ID);
alert(textbox.value);
}”

//regestering the Javascript
this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "Javascriptkey", script);
base.OnPreRender(e);

}

Then call this javascript function on the button click as discussed in step 2 above.

Communicate cross Page WebParts using Query Strings

If you want to communicate with a webpart which is not in the current page, then you got a problem. If you want to pass a set of rows to the webpart, then go for cross webpart connections. Details of cross webpart Page connection can be found in below link
http://msdn.microsoft.com/en-us/library/ms469765.aspx

But If you have less number of parameters to pass, then I suggest you go for Query strings. In this article, I am describing how to work with query strings.

1.Redirecting the user from the donor webpart page to the Page having Acceptor webpart

string desiredURL = "http://www.moss2k:8080/ITServices";
//Replace desired Url with your page url of the acceptor webpart
string business= “Health”;
this.Page.Response.Redirect(WebUrl + "?mode=f&b=" + business);


Here I have passed two query strings,one is “mode” whose value is “f” and “b” whose value is assigned to a variable “business”

2.Getting the Values of query String in Acceptor webpart

this.Page.Request.QueryString[“mode”].ToString();
this.Page.Request.QueryString[“b”].ToString();


this will give you value “f” and “health” respectively