如果未选中任何复选框,则提供默认值

时间:2018-02-13 21:16:59

标签: c# html asp.net sharepoint-2013

因此,当我没有选择复选框选项时,我在尝试提交表单时会收到错误。我尝试在下面使用else语句,但是当我这样做时,如果我选择了一个复选框选项,它只输入else语句。 我删除了else语句,因为它没有用,但我有的是:

  if (checkItem.Selected)
  { //build string
    valueFormat3 = string.Concat("#", checkItem.Value, ";");
    finalString3 += valueFormat3;
  } 
  else
   {
       finalString3 = ("#nothing selected#");
   }

错误我没有使用else语句:

startIndex cannot be larger than length of string.
Parameter name: startIndex
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.ArgumentOutOfRangeException: startIndex cannot be larger than length of string.
Parameter name: startIndex

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace: 


[ArgumentOutOfRangeException: startIndex cannot be larger than length of string.
Parameter name: startIndex]
   System.String.Substring(Int32 startIndex, Int32 length) +12995315
   EventFormWeb.Default.submit(Object sender, EventArgs e) +1364
   System.Web.UI.WebControls.Button.OnClick(EventArgs e) +9815014
   System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +204
   System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +12
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +15
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +35
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1639

C#:

        string valueFormat3 = string.Empty;
        string finalString3 = string.Empty;

        //Get values of checklist items
        foreach (System.Web.UI.WebControls.ListItem checkItem in tSChoices.Items)
        {
            if (checkItem.Selected)
            { //build string
                valueFormat3 = string.Concat("#", checkItem.Value, ";");
                finalString3 += valueFormat3;
            } 
        }
        //trim final string of leading and ending characters
        finalString3 = finalString3.Substring(1, finalString3.Length - 2);

        //pass choices as string, name sp name
        entry["tSChoices"] = finalString3;

        entry.Update(); // local item changes
        clientContext.Load(entry);
        clientContext.ExecuteQuery(); 
    }

HTML / ASP.NET:

<div class="row">
  <div class="col-md-12">
    Technical Services
      <asp:DropDownList class="form-control" id="technicalServices" runat="server" onchange="dropthree.showHideInput(this, 'Yes')">
         <asp:ListItem>No</asp:ListItem>
         <asp:ListItem>Yes</asp:ListItem>
     </asp:DropDownList>
  <div class="col-md-12" id="dropthree" runat="server" style="display:none;">
     <div class="row well well-sm">
        <div class="col-xs-12 checkbox checkbox-primary">
            <asp:CheckBoxList ID="tSChoices" CssClass="styled" RepeatColumns="2" Type="checkbox" runat="server">
            <asp:ListItem Text="Mic" Value="Mic" />
            <asp:ListItem Text="Vid. Conference" Value="Video Conference" />
            <asp:ListItem Text="Audio" Value="Audio" />
            <asp:ListItem Text="Streaming" Value="Streaming" />
            <asp:ListItem Text="WiFi-Guest" Value="Wifi-Guest-Network" />
            <asp:ListItem Text="Projector" Value="Projector" />
            <asp:ListItem Text="Payment-Portal" Value="Payment Portal" />
            <asp:ListItem Text="Laptop" Value="Laptop" />
          </asp:CheckBoxList>
      </div>
        </div>
     </div>
  </div>
</div>

2 个答案:

答案 0 :(得分:0)

我可以告诉你你的例外来自哪里:

 //trim final string of leading and ending characters
     finalString3 = finalString3.Substring(1, finalString3.Length - 2);

   //So what happens in this call above if finalString3 is still empty????
   //Then you are making a call to .Substring() with start index of 1 and
   //has Length of 0 - 2 which equals -2
   //You can't have a string with a length of a negative number, plus
   //strings are basically character arrays and are zero indexed
   //So if you want to start at the first character you use zero

要回答应该解决异常的问题部分,请将您的代码行更改为:

 //trim final string of leading and ending characters
   finalString3 = finalString3.Length < 3 ? "#nothing selected#" : finalString3.Substring(1, 
   finalString3.Length - 2);

答案 1 :(得分:0)

修改C#代码如下:

string valueFormat3 = string.Empty;
string finalString3 = string.Empty;

//Get values of checklist items
foreach (System.Web.UI.WebControls.ListItem checkItem in tSChoices.Items)
{
    if (checkItem.Selected)
    { //build string
        valueFormat3 = string.Concat("#", checkItem.Value, ";");
        finalString3 += valueFormat3;
    }
}          
if (!string.IsNullOrEmpty(finalString3))
{
    //trim final string of leading and ending characters
    finalString3 = finalString3.Substring(1, finalString3.Length - 2);
}