how to get data from client-side assembly in one step?

J

Jim Hammond

I can get data from a client-side assembly to the server in two manual
steps, but I need to be able to do it in one step.

Step 1:
The user presses the manually coded "Step 1" button, which calls the
JavaScript function.
The function copies the data into a server textbox control.

Step 2:
The user presses the VS generated "Step 2" button, which calls the
server-side event handler "Button1_Click".
Button1_Click can read and thus process the contents of the server control.


<body MS_POSITIONING="GridLayout">

<OBJECT id="myControl1" height="288" width="312"
classid="ClientSideAssembly.dll#ClientSideAssembly.myControl"
name="myControl1" VIEWASTEXT>
</OBJECT>

<form id="Form1" name="Form1" method="post" runat="server" VIEWASTEXT>
<input onclick="doScript();" type="button" value="step 1" style="WIDTH:
100px; HEIGHT: 24px">
<asp:TextBox id="TextBox1" style="Z-INDEX: 101; LEFT: 11px; POSITION:
absolute; TOP: 400px"
runat="server" Width="359px"></asp:TextBox>
<asp:Button id="Button1" style="Z-INDEX: 103; LEFT: 375px; POSITION:
absolute; TOP: 400px"
runat="server" Text="Step 2" Width="82px"></asp:Button>
</form>

<script language="javascript">
function doScript()
{
// This line successfully uses the client-side assembly
// to read text from a client file and assign it to a server control,
// but this doesn't trigger the TextBox1_TextChanged event for that
server control.
Form1.TextBox1.value = myControl1.FileText;
}
</script>

</body>
 
F

Fred W

You have to connect both a client event handler and a server event
handler to the same button.
I recently implemented this functionality. Below is a test harness
that demonstrates how to pass a value from client to server using
either a server control (asp:button) or a client control
(HtmlInputButton)

(HTML)
********************************************************
<HTML>
<HEAD>
<title>ClientServerCrossover</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio 7.0">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema"
content="http://schemas.microsoft.com/intellisense/ie5">
<script language="javascript">
function client_func(source){
document.form1.hidden1.value = source;
//document.form1.hidden1.style.color
}
</script>
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="form1" method="post" runat="server">
<input ID="hidden1" runat="server" type="hidden">&nbsp;
<asp:Button id="aspButton" runat="server" style="Z-INDEX: 101;
LEFT: 43px; POSITION: absolute; TOP: 58px" Width="120px" Text="Use asp
button"></asp:Button>
<input ID="htmlInput" type="button"
onserverclick="EitherButton_Click" onclick="client_func('html
input');" runat="server" style="Z-INDEX: 102; LEFT: 210px; WIDTH:
104px; POSITION: absolute; TOP: 58px; HEIGHT: 24px" value="Use html
Input">
<asp:TextBox id="txtOutput" style="Z-INDEX: 103; LEFT: 16px;
POSITION: absolute; TOP: 116px" runat="server"
Width="333px"></asp:TextBox>
</form>
</body>

(codebehind)
***********************************************************************
//the value(text) property can be assigned from client function for
later use by server code
protected System.Web.UI.HtmlControls.HtmlInputHidden hidden1;
//the client and server event handlers are specified in HTML
protected System.Web.UI.HtmlControls.HtmlInputButton htmlInput;
//the client event handler is added in Page_Load, the server event
handler in OnInit
protected System.Web.UI.WebControls.Button aspButton;
protected System.Web.UI.WebControls.TextBox txtOutput;

private void Page_Load(object sender, System.EventArgs e)
{
//if the control is a webcontrol (asp:) then client script event
handlers
//must be added at runtime
if (!IsPostBack)
aspButton.Attributes.Add("onclick", "client_func('asp button')");
}

public void EitherButton_Click(object sender, System.EventArgs e)
{
//both the htmlInput and the aspButton are wired up to use this
handler on postback
txtOutput.Text = ("value passed from client to server using " +
hidden1.Value.ToString());
}

override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
//add server event handler for server control
aspButton.Click += new
System.EventHandler(this.EitherButton_Click);
this.Load += new System.EventHandler(Page_Load);
}

- Fred W
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top