detect raadio button submit value and assign value based on submission

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>")));
                  
                  
            }
      }
}

LVL 25
James RodgersWeb Applications DeveloperAsked:
Who is Participating?
 
RejojohnyConnect With a Mentor Commented:
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
 
divinewind80Commented:
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
 
James RodgersWeb Applications DeveloperAuthor Commented:
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
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
divinewind80Commented:
It is very possible the C# syntax is different than VB.  I'll check on that...
0
 
RejojohnyConnect With a Mentor Commented:
use
radiobuttonlistid.SelectedValue
to get the selected item from a radiobuttonlist

Rejo
0
 
James RodgersWeb Applications DeveloperAuthor Commented:
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
 
RejojohnyConnect With a Mentor Commented:
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
 
James RodgersWeb Applications DeveloperAuthor Commented:
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
 
James RodgersWeb Applications DeveloperAuthor Commented:
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
 
RejojohnyCommented:
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
 
James RodgersWeb Applications DeveloperAuthor Commented:
>>and VS does that for you automatically .. making us more lazy
more of a hand coder.... dont have VS
0
 
RejojohnyCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.