Xylon wrote on Feb 18
th, 2014 at 3:37pm:
Btw if I would like to set the subject dropdownlist value from database(exp in your code the names and values of "text one, text two, two three" are from databse, so pass database value into 'function()'??)
Hi,
The drop-down expects an array, which contains objects with two fields -
value and
text. The
value field is used as the actual appointment subject value when the form result is evaluated; and the
text field is used for displaying the text in the drop-down.
To populate the items in the select from a database, you could use an ajax call from the client to query the database from the server. To achieve this modify the
populateSubjectItems function from the snippet above to something like:
var populateSubjectItems = function() {
// Create an object that will contain the result from the AJAX call
var obj = {};
// Fetch the data with an call to the server's GetSubjects webmethod
$mindfusion.ajax({
type: "POST",
async: false,
data: null,
url: "Default.aspx/GetSubjects",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) {
obj.value = msg.d;
},
error: function(msg) {
obj.error = { status: msg.status, statusText: msg.statusText };
}
});
if (obj.value) {
// If the query was successful deserialize the result and return it.
var items = Sys.Serialization.JavaScriptSerializer.deserialize(obj.value);
return items;
}
}
The code on the server that will query the database (uses an MSSql client in this example):
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Web.Services;
...
[WebMethod]
public static string GetSubjects()
{
StringBuilder result = new StringBuilder("[");
try
{
using (SqlConnection connection = new SqlConnection(myConnectionString))
{
// Fetch the data from the database
SqlCommand command = new SqlCommand(
"SELECT DISTINCT [SubjectText] FROM [SubjectsTable]", connection);
connection.Open();
SqlDataReader reader = command.ExecuteReader(CommandBehavior.Default);
while (reader.Read())
{
// Format the value from the database for the drop-down, i.e.:
// { value: valueFromDB, text: valueFromDB }
result.AppendFormat("{value:\"{0}\",text:\"{0}\"},", reader[0].ToString());
}
}
}
catch { }
if (result[result.Length - 1] == ',')
result.Remove(result.Length - 1, 1);
result.Append("]");
// Return the resulting JSON string to the client.
// It should look like:
// [{value:"First Value",text:"First Value"},{value:"Second Value",text:"Second Value"}]
return result.ToString();
}
In order to use WebMethods you should enable partial page rendering. Set the
EnablePartialRendering property of the
ScriptManager in your aspx code:
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePartialRendering="true" />
Note that with the above snippets the database will be queried every time the popup form is displayed, so it's not very efficient. You may need to cache the result from the query or do something else that is suitable for your scenario.
Regards,
Lyubo