Set enum value via dropdown list select (ddl)
There are three files: index.aspx
serverInfo.cs
setup.aspx.vb
My enum is in:
---------------
//Class:serverInfo.cs
---------------
public enum ServerVersion
{
Exchange2007SP1 = 0,
Exchange2011 = 1,
}
// <summary>
/// Creates a new Service Provider for a specific Server version
/// </summary>
/// <param name="serverVersion">Version of the Exchange Server</param>
public ExchangeServiceProvider(ExchangeServerVersion serverVersion)
{
this._service = new ExchangeService((ExchangeVersion)serverVersion);
}
---------------
//Class:setup.aspx.vb
---------------
Private Sub manualConnect()
Dim accNameM As String = txtAccName2.Text
Dim passM As String = txtPass2.Text
Dim exVer As String = ddlExVersion.SelectedValue
Dim servURL As String = txtURL.Text
'----------------------------------'
conToExchange = New ymp.Utility.Services.Exchange.ExchangeServiceProvider()
conToExchange.Credentials = New System.Net.NetworkCredential(accNameM, passM)
'----------------------------------'
'FOR MANUAL CONNECTION
conToExchange.Connect(servURL)
conToExchange.Connect = [Enum].Parse(exVer)
'---------------------
Dim connected As Integer = conToExchange.Connect(servURL)
'--------------------------------------------------
'CHECK CONNECTION
conToExchange.Connect(exVer)
'TRY TO CONNECT TO EXCHANGE USING AUTODISCOVER,ELSE COULD NOT CONNECT
If connected Then
'GO TO SUCCESS PAGE
phManualSetup.Visible = False
phSuccess.Visible = True
Else
phError.Visible = True
End If
End Sub
-------------
//Markup:index.aspx
<asp:DropDownList ID="ddlExVersion" runat="server">
<asp:ListItem Selected="True" Value="Ex2010SP1">Exchange 2010 SP1</asp:ListItem>
<asp:ListItem Value="Ex2011">Exchange 2011</asp:ListItem>
</asp:DropDownList><span></span>
What I want to do:
See which version is selected via dropdown list, compare this to the value in the enum store in var and add to my connecti开发者_JAVA百科on check.
Any help or suggestion?
Thank you in advance.
Thanks
If I understand the question properly you need to see if the selected value is a valid/defined enum value, you can do something like this. Place this method in the serverInfo.cs.
/// <summary>
/// Returns the Enum Value based on String provided
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public int? GetEnumValue(string value)
{
ServerVersion newEnumVar;
bool parsed = Enum.TryParse<ServerVersion>(value, out newEnumVar);
if (parsed)
{
return Convert.ToInt32(newEnumVar);
}
return null;
}
and in setup.aspx.vb
Dim enumValue as Nullable(Of Integer)
enumValue = conToExchange.GetEnumValue(ddlExVersion.SelectedValue)
精彩评论