?
Solved

detect raadio button submit value and assign value based on submission

Posted on 2007-10-19
12
Medium Priority
?
240 Views
Last Modified: 2012-06-21
first time doing asp.net
i have a form
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
      <!---
      <asp:radiobuttonlist id="RadioList1" runat="server" RepeatDirection="Horizontal">
        <asp:ListItem Value="start">Start</asp:ListItem>
            <asp:ListItem Value="stop">Stop</asp:ListItem>
            <asp:ListItem Value="restart">Restart</asp:ListItem>
      </asp:radiobuttonlist>
 --->
      
      
      
      <asp:RadioButton id="rdoStop" Text="Stop" GroupName="rdoAction" runat="server"/><br />
      <asp:RadioButton id="rdoStart" Text="Start" GroupName="rdoAction" runat="server"/><br />
      <asp:RadioButton id="rdoRestart" Text="Restart" GroupName="rdoAction" runat="server"/><br />
      <asp:button type="submit" text="Run File" runat="server"/>
<!--
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    <asp:Button ID="Button1" runat="server" Text="Start" value="Start" />
       <asp:Button ID="Button2" runat="server" Text="Stop" value="Stop" />
       <asp:Button ID="Button3" runat="server" Text="Resume" value="Resume" />--->
   
    </div>
    </form>
</body>
</html>
i need to be able to detect teh value of the submitted radio button and based on that value assign a value to a string, i am gettign nothing but errors

here is teh processing page
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

partial class _Default : System.Web.UI.Page{
    protected void Page_Load(object sender, EventArgs e){
        if(Page.IsPostBack){
                  if (rdoStart.Checked) then
                           string fileName="start.bat";
                  elseIf (rdoStop.Checked) then
                           string fileName="stop.bat";
                  elseIf (rdoRestart.Checked) then
                           string fileName="restart.bat";
                  else
                        string fileName="fine.bat";
                  end if

                string exec = TextBox1.Text;
                // Get the full file path
                string strFilePath = Server.MapPath(fileName);
            
                // Create the ProcessInfo object
                System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo("cmd.exe");
                psi.UseShellExecute = false;
                psi.RedirectStandardOutput = true;
                psi.RedirectStandardInput = true;
                psi.RedirectStandardError = true;
            
            
                // Start the process
                System.Diagnostics.Process proc = System.Diagnostics.Process.Start(psi);
            
                // Open the batch file for reading
            
                //System.IO.StreamReader strm = System.IO.File.OpenText(strFilePath);
                System.IO.StreamReader strm = proc.StandardError;
                // Attach the output for reading
                System.IO.StreamReader sOut = proc.StandardOutput;

                // Attach the in for writing
                System.IO.StreamWriter sIn = proc.StandardInput;
            
                // Write each line of the batch file to standard input
                /*while(strm.Peek() != -1){
                      sIn.WriteLine(strm.ReadLine());
                    }*/
            
                     sIn.WriteLine(exec);
            
                strm.Close();
            
            
                // Exit CMD.EXE
                string stEchoFmt = "# {0} run successfully. Exiting";
            
                sIn.WriteLine(String.Format(stEchoFmt, strFilePath));
                sIn.WriteLine("EXIT");
            
                // Close the process
                proc.Close();
            
                // Read the sOut to a string.
                string results = sOut.ReadToEnd().Trim();
            
                // Close the io Streams;
                sIn.Close();
                sOut.Close();
            
                // Write out the results.
                string fmtStdOut = "<font face=courier size=0>{0}</font>";
                this.Response.Write("<br>");
                this.Response.Write("<br>");
                this.Response.Write("<br>");
                this.Response.Write(String.Format(fmtStdOut, results.Replace(System.Environment.NewLine, "<br>")));
                  
                  
            }
      }
}

0
Comment
Question by:James Rodgers
  • 5
  • 5
  • 2
12 Comments
 
LVL 9

Expert Comment

by:divinewind80
ID: 20110568
One way to tell which item in a radiobuttonlist is selected is as follows:

Dim x as long
For x = 0 to radiobuttonlistid.items.count - 1
If radiobuttonlistid.items(x).selected = true then
Your code here.
End If
End

Does that help?
0
 
LVL 25

Author Comment

by:James Rodgers
ID: 20110893
tried your suggestion and this is what i got
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

Compiler Error Message: CS1002: ; expected

Source Error:

 

Line 13:     protected void Page_Load(object sender, EventArgs e){
Line 14:         if(Page.IsPostBack){
Line 15:                   For x = 0 to radiobuttonlistid.items.count - 1
Line 16:                         If radiobuttonlistid.items(x).selected = true then
Line 17:                               string fileName="start.bat";
 
0
 
LVL 9

Expert Comment

by:divinewind80
ID: 20110959
It is very possible the C# syntax is different than VB.  I'll check on that...
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 26

Assisted Solution

by:Rejojohny
Rejojohny earned 2000 total points
ID: 20110974
use
radiobuttonlistid.SelectedValue
to get the selected item from a radiobuttonlist

Rejo
0
 
LVL 26

Accepted Solution

by:
Rejojohny earned 2000 total points
ID: 20111041
i noticed that you have commented your radiobutonlist in the aspx code and changed them to individual radio button .. i would advice to revert back to the radiobuttonlilst .. that is uncomment the code
<div>
      <asp:radiobuttonlist id="RadioList1" runat="server" RepeatDirection="Horizontal">
        <asp:ListItem Value="start">Start</asp:ListItem>
            <asp:ListItem Value="stop">Stop</asp:ListItem>
            <asp:ListItem Value="restart">Restart</asp:ListItem>
      </asp:radiobuttonlist>

and comment
<!--
<asp:RadioButton id="rdoStop" Text="Stop" GroupName="rdoAction" runat="server"/><br />
      <asp:RadioButton id="rdoStart" Text="Start" GroupName="rdoAction" runat="server"/><br />
      <asp:RadioButton id="rdoRestart" Text="Restart" GroupName="rdoAction" runat="server"/><br />
      <asp:button type="submit" text="Run File" runat="server"/>
-->

then int he code behing
 protected void Page_Load(object sender, EventArgs e){
        if(Page.IsPostBack)
        {
                  if (RadioList1.SelectedValue == "start")  
... and so on and so forth ..

I have noticed that your syntax is a mix of both vb and C# .. i also notice that your code-bind is named as .cs and so you want to code in C# .. if yes, please make the correction accordingly ..
for eg, the if .. else condition for C# does not have "then" and "endif", C# is case-sensitive and so be careful with the controlnames, variables etc

Rejo
0
 
LVL 25

Author Comment

by:James Rodgers
ID: 20111776
changed processing code to
string fileName="fine.bat";

                  If (radiobuttonlistid.SelectedValue == "start")
                           string fileName="start.bat";
                  Else If (radiobuttonlistid.SelectedValue == "stop")
                           string fileName="stop.bat";
                  Else If (radiobuttonlistid.SelectedValue == "restart")
                           string fileName="restart.bat";

and i am getting
Compiler Error Message: CS1002: ; expected
If (radiobuttonlistid.SelectedValue == "start")
with teh highlighted line being teh first if


>>I have noticed that your syntax is a mix of both vb and C# .. i also notice that your code-bind is named as .cs and so you want to code in C# .. if yes, please make the correction accordingly ..
for eg, the if .. else condition for C# does not have "then" and "endif", C# is case-sensitive and so be careful with the controlnames, variables etc
complete asp.net noob, no idea what you are ferering to, got the code from adownload to run a batch file, trying to convert it to run a batchfile based on roadio button selection on a form....

thanks for the help
0
 
LVL 26

Assisted Solution

by:Rejojohny
Rejojohny earned 2000 total points
ID: 20112106
If (radiobuttonlistid.SelectedValue == "start")
has to be
if (radiobuttonlistid.SelectedValue == "start")

Note the "if" statement
as I said C# is case sensitive ...

there are lot of simialr correction that need to be done on the code .. where is the original code and what changes do you want? But do note that using this forum, we will not be able to correct all the errors or write complete working code .. here most of the answers you will get is on how to correct your specific problems/errors ..

Rejo
0
 
LVL 25

Author Comment

by:James Rodgers
ID: 20112197
thank you so much!
working now
quick followup
should the if's be written
if (radiobuttonlistid.SelectedValue == "start"){
conditional code;
} else if (condition){
other conditional code;
} else{

}
i prefer that coding style, even on single statement if's
0
 
LVL 25

Author Comment

by:James Rodgers
ID: 20112237
followup question here
http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/Q_22905646.html

also any recommendations for intro to .NET programming?
0
 
LVL 26

Expert Comment

by:Rejojohny
ID: 20113321
wht style? do you mean the
if (contition) { ... brackets on the same line .. no problem with how you write the code as long as you are comfortable with it .. I would just recommend proper indentation .. and VS does that for you automatically .. making us more lazy .. :-) ..

Thanks for the points .. glad that you got that working and to be of some help ..

Rejo
0
 
LVL 25

Author Comment

by:James Rodgers
ID: 20122564
>>and VS does that for you automatically .. making us more lazy
more of a hand coder.... dont have VS
0
 
LVL 26

Expert Comment

by:Rejojohny
ID: 20123226
try to get Vs or atleast the free version, I think it is called the web developer .. it will help you a lot ..

Rejo
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses
Course of the Month14 days, 19 hours left to enroll

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question