Populate ASPxTextBox and ASPxListBox from Code Behind - devexpress

im using the Devexpress ASPxGridView for a project but are having problems to populate a custom EditForm with data.
It looks as follows
<Templates>
<EditForm>
Company Name:
<dx:ASPxTextBox ID="CompanyName" runat="server" Value="<% #Bind('CompanyName') %>" />
Parent:
<dx:ASPxListBox ID="ParentGuid" runat="server" Value="<% #Bind('ParentGuid') %>" />
</EditForm>
</Templates>
Normally I'd write something like
protected void ASPxGridView1_StartRowEditing(object sender, DevExpress.Web.Data.ASPxStartRowEditingEventArgs e)
{
var CompanyList = db.companies.OrderBy(x => x.CompanyName).ToList();
ASPxListBox ParentGuid = (ASPxListBox)ASPxGridView1.FindControl("ParentGuid");
ParentGuid.DataSource = CompanyList;
ParentGuid.DataBind();
ASPxTextBox CompanyName = (ASPxTextBox)ASPxGridView1.FindControl("CompanyName");
CompanyName.Text = "Some company name";
}
But that wont work. Any advice where to start? The documentation doesn't really cover custom forms that well :/
Thanks!
== UPDATE ==
Tried using the onhtmleditformcreated="ASPxGridView1_HtmlEditFormCreated" with the method
protected void ASPxGridView1_HtmlEditFormCreated(object sender, ASPxGridViewEditFormEventArgs e)
{
Control CompanyName = ASPxGridView1.FindEditFormTemplateControl("CompanyName");
if (CompanyName != null)
{
ASPxTextBox CompanyNameEdit = CompanyName as ASPxTextBox;
CompanyName.Text = "Some Co";
}
}
The fact that I use Value="<% #Bind('CompanyName') %>" messes stuff up a bit. If i remove the Bind the boxes gets populated but then I have no way of fetching the data in them. Any way around this?

You should use a slightly different approach:
Use the HtmlEditFormCreated event to set editors properties. To obtain editor instances, use the gridView's FindEditFormTemplateControl method.

The answer is...
To populate a ASPxComboBox called CompanyList
protected void ASPxGridView1_HtmlEditFormCreated(object sender, ASPxGridViewEditFormEventArgs e)
{
Control ParentGuidControl = ASPxGridView1.FindEditFormTemplateControl("ParentGuid");
if (ParentGuidControl != null)
{
ASPxComboBox ParentGuid = (ASPxComboBox)ParentGuidControl;
var CompanyList = db.companies.OrderBy(x => x.CompanyName);
ParentGuid.TextField = "CompanyName";
ParentGuid.ValueField = "CompanyGuid";
ParentGuid.DataSource = CompanyList;
ParentGuid.DataBind();
}
}
BUT!
If you have a custom form like i had
<Templates>
<EditForm>
Company Name:
<dx:ASPxTextBox ID="CompanyName" runat="server" Value="<% #Bind('CompanyName') %>" />
Parent:
<dx:ASPxComboBox ID="ParentGuid" runat="server" Value="<% #Bind('ParentGuid') %>" />
</EditForm>
</Templates>
you wont be able to populate it from code-behind, the #bind method is in the way and overwrite any other incoming value. However if you are not planning to populate them from code behind here is a neat trick to fetch the data...
protected void ASPxGridView1_RowInserting(object sender, DevExpress.Web.Data.ASPxDataInsertingEventArgs e)
{
IDictionaryEnumerator enumerator = e.NewValues.GetEnumerator();
string CompanyName = string.Empty;
Guid ParentGuid = Guid.Empty;
enumerator.Reset();
while (enumerator.MoveNext())
if (enumerator.Key.ToString() == "CompanyName")
CompanyName = enumerator.Value.ToString();
else if (enumerator.Key.ToString() == "ParentGuid")
ParentGuid = new Guid(enumerator.Value.ToString());
// Do insert trick here
}
But if you want to fill some of the form values from code-behind ensure there are no #bind methods in the EditForm
<Templates>
<EditForm>
Company Name:
<dx:ASPxTextBox ID="CompanyName" runat="server" />
Parent:
<dx:ASPxComboBox ID="ParentGuid" runat="server" />
</EditForm>
</Templates>
Populate as described in the top of this post and fetch the values like this
protected void ASPxGridView1_RowInserting(object sender, DevExpress.Web.Data.ASPxDataInsertingEventArgs e)
{
string CompanyName = string.Empty;
Guid ParentGuid = Guid.Empty;
// This method is a bit more secure
Control CompanyNameControl = ASPxGridView1.FindEditFormTemplateControl("CompanyName");
if (CompanyNameControl != null)
{
ASPxTextBox CompanyNameTb = (ASPxTextBox)CompanyNameControl;
CompanyName = CompanyNameTb.Text.ToString();
}
// A bit less secure, but lesser code
ASPxComboBox ParentGuidControl = (ASPxComboBox)ASPxGridView1.FindEditFormTemplateControl("ParentGuid");
ParentGuid = new Guid(ParentGuidControl.SelectedItem.Value.ToString());
// Do insert...
}
Have fun

Related

Dropdown Selected IndexChanged is not firing

I have one dropdownlist and its autopostback property is set to true.But when value is changed the selectedindexchanged property is not fired instead it is always going to pageload.Please tell what is the issue.
<asp:DropDownList ID="ddlVendor" CssClass="ddl" runat="server"
OnSelectedIndexChanged="ddlVendor_SelectedIndexChanged" AutoPostBack="true">
protected void ddlVendor_SelectedIndexChanged(object sender, EventArgs e)
{
List<ProcurementItem> vendorsList = new List<ProcurementItem>();
vendorsList = (List<ProcurementItem>)ViewState["VendorList"];
string ID = string.Empty;
string accountID = string.Empty;
int? accountType = null;
if (ddlVendor.SelectedIndex > 0)
{
ID = ddlVendor.SelectedValue;
ProcurementClient procurementClient = new ProcurementClient();
List<ProcurementContract> contractList =
procurementClient.GetContractList(Convert.ToInt32(ID), null);
contractList = contractList.Where(i => i.Status == 4).ToList();
ddlContracts.DataSource = contractList;
ddlContracts.DataTextField = "ContractIDName";
ddlContracts.DataValueField = "ContractID";
ddlContracts.DataBind();
ddlContracts.Items.Insert(0, "");
}
}
Add following property in DropDownList
1.ViewStateMode="Enabled"
2.EnableViewState="true"
3.AutoPostBack="true"
try this
<asp:dropdownlist id=ddltrim width="100%" Runat="server" AutoPostBack="True" EnableViewState="True" onselectedindexchanged="ddltrim_SelectedIndexChanged">
Make Sure That your DropDownList within the Fom tag
<form>
//
</form>
dear check the causesvalidation= false
may be this is the problem
if you have any validations in your page dear
Thanks
:D
If you are filling the dropdown list from database then, make sure that the DataBind() method of dropdown list is called only when its not a post back as...
protected void Page_Load(object sender, EventArgs e)
{
...
If(!Page.IsPostBack)
{
......
dropdownlist.DataBind();
.....
}
...
}
Hope this helps :)

How to change the value of input hidden on server-side

I have a form with hidden fields:
<form id="Form1" runat="server" style="width: 100%; height: 100%; overflow: hidden" onsubmit="return false;">
<div>
<input type="hidden" runat="server" id="TrackColors" value=""/>
<input type="hidden" runat="server" id="Relogin" value=""/>
</div>
</form>
After Page_Load() on the server-side is called function:
protected void SomeFunction()
{
Dictionary<int, int> trackColors = new Dictionary<int, int>();
if (!String.IsNullOrEmpty(TrackColors.Value))
trackColors = ReadValues(TrackColors.Value);
//if value is null or empty it's assigned to a different
TrackColors.Attributes["value"] = FormValues(trackColors); //FormValues() return string
//change is visible
}
string FormValues(Dictionary<int, int> values)
{
string result = "";
if (values == null || values.Count == 0)
return result;
foreach (KeyValuePair<int, int> p in values)
result += p.Key + "##" + p.Value + "^^";
result = result.TrimEnd('^');
return result;
}
If I change the selected field of ComboBox, the function is called:
<dx:ASPxTextBox ID="ColorTrackCarID" Visible="false" Text='<%# Eval("CarId") %>' />
<dx:ASPxComboBox ID="ASPxComboBox1" runat="server" SelectedIndex='<%# Eval("TrackColor") %>'
ValueType="System.String" Width="30" ShowImageInEditBox="true"
ondatabinding="ASPxComboBox1_DataBinding">
<ClientSideEvents SelectedIndexChanged="function (s,e) {
if (window.TrackColorChanged != null)TrackColorChanged(s,e); }" />
</dx:ASPxComboBox>
function TrackColorChanged(s, e) {
var TrackColors = document.getElementById('TrackColors');
if (TrackColors == null || TrackColors.value == "")
return values;
//values is always emply
}
I understand the value of the form fields are not passed back to the client-side. The question is: How to pass these values ​​back?
And if I change the value on the server-side in Page_Load (), then the client can see everything, that is,
protected void Page_Load(object sender, EventArgs e)
{
TrackColors.Attributes["value"] = "bla-bla-bla";
//All changes are visible on the client-side
}
Thank you for your attention.
To make it even easier, replace your hidden fields with the control:
<asp:HiddenField id="X" runat="server" />
Which you can set the value on it directly:
X.Value = "XYZ";
This value can be passed from client to server, and vice versa, and works very easily. Not that you can't use a server-side input, but HiddenField handles a lot of that for you.
EDIT: Also, are you sure you're not overwriting the value? If you are doing this:
protected void Page_Load(object sender, EventArgs e)
{
TrackColors.Attributes["value"] = "bla-bla-bla";
//All changes are visible on the client-side
}
This will always change the value to "bla-bla-bla". You would want to wrap it in if (!Page.IsPostback) if you initialize it on page load.

asp.net editing data

I'm reading data from database and showing it in a page for editing:
<h2>Create new topic:
<asp:Label ID="_lblTopicName" runat="server" Text=""></asp:Label></h2>
<p>
Edit Level:
<br/>
<asp:DropDownList ID="_dtlEditRole" runat="server"></asp:DropDownList>
<br/>
View Level:
<br/>
<asp:DropDownList ID="_dtlViewRole" runat="server"></asp:DropDownList>
<br/>
<asp:TextBox ID="_tbxTopicText" TextMode="MultiLine" runat="server" Height="204px"
Width="885px"></asp:TextBox>
</p>
<asp:Button ID="_btnSaveTopic" runat="server" Text="Save" onclick="_btnSaveTopic_Click" />
I fill the fields in Page_PreRender() like so:
private string _topicString;
private Topic _topic = null;
private Topics_GetTopicByTopicResult _findTopicResults = null;
protected void Page_PreRender(object sender, EventArgs e)
{
// Load the User Roles into checkboxes.
_dtlEditRole.DataSource = Roles.GetAllRoles();
_dtlEditRole.DataBind();
_dtlViewRole.DataSource = Roles.GetAllRoles();
_dtlViewRole.DataBind();
_topicString = Request.QueryString["Topic"];
if (String.IsNullOrEmpty(_topicString))
{
Response.Redirect("~/Default.aspx");
}
else
{
_topic = new Topic();
_findTopicResults = _topic.FindTopic(_topicString);
if (_topic != null)
{
// Check if the user has permission to access
if (RoleHelper.IsEditAllowed(_findTopicResults.ViewRoleName))
{
_lblTopicName.Text = _findTopicResults.Topic;
_tbxTopicText.Text = _findTopicResults.Text;
_dtlEditRole.SelectedValue = _findTopicResults.EditRoleName;
_dtlViewRole.SelectedValue = _findTopicResults.ViewRoleName;
}
else
{
Response.Redirect("~/Error.aspx?ReturnUrl=" + HttpUtility.UrlEncode(Request.RawUrl));
}
}
else
{
Response.Redirect("~/CreateTopic.aspx?Topic=" + _topicString);
}
}
}
But now when i click _btnSaveTopic button the fields:
private string _topicString;
private Topic _topic = null;
private Topics_GetTopicByTopicResult _findTopicResults = null;
They are all NULL and im not able to update aything.
Here's my button click event:
protected void _btnSaveTopic_Click(object sender, EventArgs e)
{
_topic.UpdateTopic(_findTopicResults.ID, _findTopicResults.Topic, _tbxTopicText.Text,
_dtlViewRole.SelectedItem.Text, _dtlEditRole.SelectedItem.Text);
Response.Redirect("~/ViewPage.aspx?Topic=" + _topicString);
}
What would be the right way doing this?
ASP.NET Page Life Cycle states that Page_Init should be used to 'initialize control properties' which looks like what you are doing.
Also, it's usually good practice to breakup such large sections of code into smaller refactored methods. Try to keep the amount of code directly placed in event handlers to a minimum.
You can start by right-clicking a section of highlighted code in visual studio -> refactor -> extract method
Also, if you need more help understanding how to improve your code, you should ask a question pointing to this question on the code review site: here
You are re-binding the drop down list (and therefore wiping out the 'SelectedValue') in your Page_PreRender method. Wrap the method in
protected void Page_PreRender(object sender, EventArgs e)
{
if( !IsPostBack){
//your current code
}
}
and it should work.

How to change the data in Telerik's RadGrid based on Calendar's selected dates?

I was creating another usercontrol with Telerik's RadGrid and Calendar.
<%# Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %>
<table class="style1">
<tr>
<td>From</td>
<td>To</td>
</tr>
<tr>
<td><asp:Calendar ID="Calendar1" runat="server" SelectionMode="Day"></asp:Calendar></td>
<td><asp:Calendar ID="Calendar2" runat="server" SelectionMode="Day"></asp:Calendar></td>
</tr>
<tr>
<td><asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click" /></td>
<td><asp:Button ID="btnClear" runat="server" Text="Clear" OnClick="btnClear_Click" /></td>
</tr>
</table>
<telerik:RadGrid ID="RadGrid1" runat="server">
<MasterTableView CommandItemDisplay="Top"></MasterTableView>
</telerik:RadGrid>
and I am using Linq in code-behind:
Entities1 entities = new Entities1();
public static object DataSource = null;
protected void Page_Load(object sender, EventArgs e) {
if (DataSource == null) {
DataSource = (from entity in entities.nsc_moneytransaction
select new {
date = entity.transaction_date.Value,
username = entity.username,
cashbalance = entity.cash_balance
}).OrderByDescending(a => a.date);
}
BindData();
}
public void BindData() {
RadGrid1.DataSource = DataSource;
}
protected void btnSubmit_Click(object sender, EventArgs e) {
DateTime startdate = new DateTime();
DateTime enddatedate = new DateTime();
if (Calendar1.SelectedDate != null && Calendar2.SelectedDate != null) {
startdate = Calendar1.SelectedDate;
enddatedate = Calendar2.SelectedDate;
var queryDateRange = from entity in entities.nsc_moneytransaction
where DateTime.Parse(entity.transaction_date.Value.ToShortDateString())
>= DateTime.Parse(startdate.ToShortDateString())
&& DateTime.Parse(entity.transaction_date.Value.ToShortDateString())
<= DateTime.Parse(enddatedate.ToShortDateString())
select new {
date = entity.transaction_date.Value,
username = entity.username,
cashbalance = entity.cash_balance
};
DataSource = queryDateRange.OrderByDescending(a => a.date);
} else if (Calendar1.SelectedDate != null) {
startdate = Calendar1.SelectedDate;
var querySetDate = from entity in entities.nsc_moneytransaction
where entity.transaction_date.Value == startdate
select new {
date = entity.transaction_date.Value,
username = entity.username,
cashbalance = entity.cash_balance
};
DataSource = querySetDate.OrderByDescending(a => a.date); ;
}
BindData();
}
protected void btnClear_Click(object sender, EventArgs e) {
Calendar1.SelectedDates.Clear();
Calendar2.SelectedDates.Clear();
}
The problems are, (1) when I click the submit button. the data in the RadGrid is not changed. (2) how can we check if there is nothing selected in the Calendar controls, because there is a date (01/01/0001) set even if we do not select anything from that calendar, thus Calendar1.SelectedDate != null is not enough. =(
Thanks.
When you bind a new datasource to the grid control you need to call Rebind to have the grid show the new data. Alternatively you could use NeedDataSource event (more elegant solution in my opinion)
For your second problem try this (assuming you're using ASP.NET Calendar and not Telerik DatePicker:
if(Calendar1.SelectedDate.Date == DateTime.MinValue) {
//no date selected
} else {
//date is selected
}

Getting data from a checkbox inside a template column of asp.net gridview

This seems like something simple, but I can't seem to figure it out! I'm trying to get 2-way data-binding to work on an ASP.net page with a check box as one of the columns. How do I get the updated values (from check boxes) back from the gridview ?????
Here is my data type:
[Serializable]
public class UserRequirements
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string UserId { get; set; }
public string Email { get; set; }
public bool ThingRequired { get; set; }
}
My markup looks something like this:
<form id="form1" method="post" runat="server" >
<asp:GridView ID="UserTable" runat="server" AutoGenerateColumns="false" >
<Columns>
...
<asp:TemplateField HeaderText="Required ?">
<ItemTemplate>
<asp:CheckBox id="chkBox1" runat="server" on
Text ="Required"
checked='<%# DataBinder.Eval(Container.DataItem,"ThingRequired") %>'>
</asp:CheckBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Button id="thebutton" Text="Save Changes" OnClick="UpdateRequirements" runat="server" CausesValidation=false />
</form>
My code behind looks something like this:
List<UserRequirements > _userList = new List<UserRequirements >();
protected void Page_Load(object sender, EventArgs e)
{
_userList = data_layer.GetUserRequirments();
this.UserTable.DataSource = _userList;
this.UserTable.DataBind();
}
Eventually, I will call something like this, but I don't know where this should go or how to get the values back from the gridview:
void UpdateRequirements(object sender, EventArgs e)
{
_userList = ???????????? // How do I get the data?
data_layer.UpdateUserRequirements( _userList );
}
foreach (GridViewRow di in GridView1.Rows)
{
HtmlInputCheckBox chkBx = (HtmlInputCheckBox)di.FindControl("chkBox1");
if (chkBx != null && chkBx.Checked)
{
/// put your code here
}
}
try something like this to get the value on change:
protected void OnCheckedChanged(object sender, EventArgs e)
{
CheckBox c = (CheckBox)sender as CheckBox;
string checkBoxId = c.ID;
bool checkBoxValue = c.Checked;
//update database
}
[EDIT]
If you want to get all the values from the rows in the grid in one go, you will need to bind the checkboxes using the Id for the row or item in your list of UserRequirements, so in your grid do something like this:
<asp:CheckBox ID="<%# Eval('Id') %>" />
then on postback, iterate through the items in the UserRequirements list matching the object/item Id with the Ids of the checkboxes in the grid .. something like this:
foreach (UserRequirement item in Requirements)
{
Control c = grid.FindControl(item.Id);
CheckBox cbx = c as CheckBox;
if (cbx != null)
{
bool value = cbx.Checked;
//update db
}
}
Note: you may need to use FindControl recursively to search child controls, or do a foreach on each GridViewRow object in the grid to pickup the checkbox you are looking for.

Resources