I have created an asp.net usercontrol that should list users in a number of applications. For that purpose, the control renders a repeater (foreach application) which in turn renders a gridview (with users for that application).
The control renders fine, except the fact that columns in the gridview are not sortable. Nothing happens (no postback) when clicking the headers. Apparently, no JavaScript is rendered to perform the postback when clicking the header.
This is the code:
[DefaultProperty("Text")]
[ToolboxData("<{0}:UserList runat=\"server\"></{0}:UserList>")]
public class UserList : WebControl
{
#region Variables
private Repeater list;
private Literal heading;
#endregion
#region Events
#endregion
#region Constructors
public UserList() : base()
{
// Default settings for list control.
list = new Repeater() { ID = "userList" };
heading = new Literal() { ID = "userListHeading" };
}
#endregion
#region Control event handlers
protected override void OnInit(EventArgs e)
{
var css = "<link href=\"" + Page.ClientScript.GetWebResourceUrl(typeof(UserList), "Web.UI.Resources.CSS.UserList.css") + "\" type=\"text/css\" rel=\"stylesheet\" />";
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "UserListCss", css, false);
base.OnInit(e);
}
public override void RenderControl(HtmlTextWriter writer)
{
heading.RenderControl(writer);
list.RenderControl(writer);
base.RenderControl(writer);
}
protected override void OnLoad(EventArgs e)
{
// set properties
list.ItemTemplate = new ListTemplate();
// attach event handlers
list.ItemDataBound += new RepeaterItemEventHandler(OnList_ItemDataBound);
// bind data
var allRoles = AdvancedRoleProvider.Instance.GetAllRoles();
String appName = (Roles.ApplicationName.Equals("*") ? " alle applikationer" : Roles.ApplicationName);
heading.Text = String.Format("<h2>{0}</h2>", "Brugere i " + appName);
if (allRoles.Count == 0)
{
heading.Text += "Der kunne ikke findes nogen brugere.";
}
else
{
list.DataSource = allRoles;
list.DataBind();
}
base.OnLoad(e);
}
#endregion
#region List control event handlers
protected void OnList_ItemDataBound(Object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
KeyValuePair<String, IList<String>> data = (KeyValuePair<String, IList<String>>)e.Item.DataItem;
Literal litApplication = (Literal)e.Item.FindControl("litApplication");
litApplication.Text = String.Format(litApplication.Text, data.Key);
GridView gvUsers = (GridView)e.Item.FindControl("gvUsers");
gvUsers.RowDataBound += new GridViewRowEventHandler(OnGridViewUsers_RowDataBound);
gvUsers.Sorting += new GridViewSortEventHandler(OnGridViewUsers_Sorting);
gvUsers.DataSource = AdvancedRoleProvider.Instance.GetApplicationUsers(data.Key);
gvUsers.DataBind();
}
}
#endregion
void OnGridViewUsers_RowDataBound(object sender, GridViewRowEventArgs e)
{
}
void OnGridViewUsers_Sorting(object sender, GridViewSortEventArgs e)
{
// TODO: Impl. sorting logic
}
#region List layout template
/// <summary>
/// Layout template class for the repeater control.
/// </summary>
public class ListTemplate : ITemplate
{
#region Constructors
public ListTemplate()
{
}
#endregion
#region Public methods
public void InstantiateIn(Control container)
{
GridView gvUsers;
gvUsers = new GridView() { ID = "gvUsers", AutoGenerateColumns = false, AllowSorting = true };
BoundField userField = new BoundField() { HeaderText = "Bruger", DataField = "UserName", SortExpression = "UserName" };
userField.HeaderStyle.CssClass = userField.ItemStyle.CssClass = "userName";
BoundField fullNameField = new BoundField() { HeaderText = "Navn", DataField = "FullName", SortExpression = "FullName" };
fullNameField.HeaderStyle.CssClass = userField.ItemStyle.CssClass = "userName";
BoundField roleField = new BoundField() { HeaderText = "Rolle", DataField = "RoleName", SortExpression = "RoleName" };
roleField.HeaderStyle.CssClass = roleField.ItemStyle.CssClass = "roleName";
BoundField adField = new BoundField() { HeaderText = "AD Gruppe", DataField = "ADGroupName", SortExpression = "ADGroupName" };
adField.HeaderStyle.CssClass = adField.ItemStyle.CssClass = "adGroupName";
gvUsers.Columns.Add(userField);
gvUsers.Columns.Add(fullNameField);
gvUsers.Columns.Add(roleField);
gvUsers.Columns.Add(adField);
container.Controls.Add(new LiteralControl("<div id=\"userList\">"));
container.Controls.Add(new Literal() { ID = "litApplication", Text="<h3>{0}</h3>" });
container.Controls.Add(gvUsers);
container.Controls.Add(new LiteralControl("</div>"));
}
#endregion
#region Event handlers
#endregion
#region Properties
#endregion
}
#endregion
}
Any help would be greatly appreciated.
I don't know if this will work or not, but you may want try adding the grid through the designer/html. Also, you may want to add the handler at the html side as well instead of the OnList_ItemDataBound. Hope this helps.
<asp:GridView ID="gvUsers" runat="server" AllowSorting="True" onsorting="OnGridViewUsers_Sorting">
Related
enter image description here
namespace Implementer
{
public partial class MainForm : Form
{
#region intit and globals
public MainForm()
{
InitializeComponent();
DevExpress.XtraGrid.Views.Grid.GridView gridView = new DevExpress.XtraGrid.Views.Grid.GridView();
var transactions = new ObservableCollection<Item>();
for (int i = 0; i <= 100; i++)
{
transactions.Add(new Item { Content = "Item " + i });
}
grdTransactions.AllowDrop = true;
grdTransactions.DataSource = transactions;
dgmWf.AddingNewItem += (s, e) => transactions.Remove(e.Item.Tag as Item);
}
Point mouseDownLocation;
GridHitInfo gridHitInfo;
#endregion
#region events
public void Mainform_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'vA_ERP4_ADMINDataSet.SYSTEM_MODULES' table. You can move, or remove it, as needed.
//Project initiation
//Fill drop down for project list
cmbProject.SelectedIndex = 0;
DataAccess dataAccess = new DataAccess(GlobalFunctions.GetConnectionString());
DataTable dtResult = dataAccess.ExecuteQueryDataSet("select MODULE_CODE, MODULE_DESC from SYSTEM_MODULES where module_is_active=1").Tables[0];
cmbProject.DisplayMember = "MODULE_DESC";
cmbProject.ValueMember = "MODULE_CODE";
cmbProject.DataSource = dtResult;
}
private void cmbProject_SelectedIndexChanged(object sender, EventArgs e)
{
lblCurrentProject.Text = cmbProject.Text;
if (cmbProject.Text != null)
{
DataAccess dataAccess = new DataAccess(GlobalFunctions.GetConnectionString());
DataTable dtTransactions = dataAccess.ExecuteQueryDataSet("select sys_trans_id, sys_trans_desc1 from WF_SYSTEM_TRANS where MODULE_CODE= '" + cmbProject.Text + "'").Tables[0];
grdTransactions.DataSource = dtTransactions;
}
}
private void btnSalesInvoice_Click(object sender, EventArgs e)
{
int sysTransId = 1001;
FillTransactionDetails(sysTransId);
}
#endregion
#region drag drop
private void grdTransactions_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left && CanStartDragDrop(e.Location))
{
StartDragDrop();
}
}
private void grdTransactions_MouseDown(object sender, MouseEventArgs e)
{
gridHitInfo = grdVTransactions.CalcHitInfo(e.Location);
mouseDownLocation = e.Location;
}
private void grdTransactions_MouseLeave(object sender, EventArgs e)
{
if (gridHitInfo != null)
gridHitInfo.View.ResetCursor();
gridHitInfo = null;
}
private bool CanStartDragDrop(Point location)
{
return gridHitInfo.InDataRow && (Math.Abs(location.X - mouseDownLocation.X) > 2 || Math.Abs(location.Y - mouseDownLocation.Y) > 2);
}
public void StartDragDrop()
{
var draggedRow = gridHitInfo.View.GetRow(gridHitInfo.RowHandle) as Item;
var tool = new FactoryItemTool(" ", () => " ", diagram => new DiagramShape(BasicShapes.Rectangle) { Content = draggedRow.Content, Tag = draggedRow }, new System.Windows.Size(150, 100), false);
dgmWf.Commands.Execute(DiagramCommandsBase.StartDragToolCommand, tool, null);
}
#endregion
#region function
private void FillTransactionDetails(int systemTransactionId)
{
//Fill document
//Fill steps
DataAccess dataAccess = new DataAccess(GlobalFunctions.GetConnectionString());
DataTable transactionDetails = dataAccess.ExecuteQueryDataSet("SELECT DOC_TYPE_DESC1 FROM WF_SYSTEM_TRANS_DT WHERE SYS_TRANS_ID=1001 and MODULE_CODE= '" + cmbProject.Text + "'").Tables[0];
transactionDetails.Rows.Add();
grdDocuments.DataSource = transactionDetails;
grdDocuments.Columns["Details"].DisplayIndex = 2;
grdDocuments.Columns["Delete"].DisplayIndex = 2;
DataTable transactionSteps = dataAccess.ExecuteQueryDataSet("select WF_STEP_DESC1 from WF_STEPS where wf_id= 10101 and MODULE_CODE= '" + cmbProject.Text + "'").Tables[0];
transactionSteps.Rows.Add();
grdSteps.DataSource = transactionSteps;
}
#endregion
}
public class Item
{
public string Content { get; set; }
}
}
I don't really know where is the mistake and have been looking at it for the past few days and searching for an answer but no luck so I'd be so happy if you could help me out. It was working without the data fetching. but after calling the data it doesn't work. drag it from the grid view and when it reaches the diagram control it would turn into a rectangle with a tag property of it's ID.. With regards to the connection string.. I created a global function to just call it on the main form.
I am working with dynamically created text fields. Most solutions I have found thus far have been related to retaining view state on postback, but I believe I have taken care of that issue. On postback, the values that are in the text fields are retained.
The issue I am having: I can't get the database values currently stored to load in the dynamic fields. I am currently calling loadUpdates() to try to do this, but unsure how to grab the data row, while also making sure I can continue to add new fields (or remove them). How can I achieve this?
"txtProjectsUpdate" is the text field, "hidFKID" is the foreign key to a parent table, and "hidUpdateID" is the hidden value of the primary key in the child table (the values I am attempting to load).
Markup:
<div>
<asp:Button ID="btnAddTextBox" runat="server" Text="Add" OnClick="btnAddTextBox_Click" />
<asp:Placeholder ID="placeHolderControls" runat="server"/>
</div>
<asp:TextBox runat = "server" ID = "hidUpdateID" />
<asp:HiddenField runat = "server" ID = "hidFKID" />
Code behind:
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack)
{
for (var i = 0; i < TextBoxCount; i++)
AddTextBox(i);
}
if (!IsPostBack)
{
DataTable dt = new DataTable();
dt = selectDetails();
tryHidFKID(hidFKID, dt.Rows[0]["fkprjNumber"].ToString());
loadUpdates();
}
}
protected void btnAddTextBox_Click(object sender, EventArgs e)
{
AddTextBox(TextBoxCount);
TextBoxCount++;
}
private int TextBoxCount
{
get
{
var count = ViewState["txtBoxCount"];
return (count == null) ? 0 : (int)count;
}
set { ViewState["txtBoxCount"] = value; }
}
private void btnRemove_Click(object sender, EventArgs e)
{
var btnRemove = sender as Button;
if (btnRemove == null) return;
btnRemove.Parent.Visible = false;
}
private void AddTextBox(int index)
{
var panel = new Panel();
panel.Controls.Add(new TextBox
{
ID = string.Concat("txtProjectUpdates", index),
Rows = 5,
Columns = 130,
TextMode = TextBoxMode.MultiLine,
CssClass = "form-control",
MaxLength = 500
});
panel.Controls.Add(new TextBox
{
ID = string.Concat("hidUpdateID", index)
});
var btn = new Button { Text = "Remove" };
btn.Click += btnRemove_Click;
panel.Controls.Add(btn);
placeHolderControls.Controls.Add(panel);
}
protected void loadUpdates()
{
DataTable dt = dbClass.ExecuteDataTable
(
"spSelectRecords", <db>, new SqlParameter[1]
{
new SqlParameter ("#vFKPrjNumber", hidFKID.Value)
}
);
AddTextBox(TextBoxCount);
TextBoxCount++;
}
protected void tryHidFKID(HiddenField hidFKID, string txtSelected)
{
try
{
hidFKID.Value = txtSelected;
}
catch
{
hidFKID.Value = "";
}
}
I have a gridview and I'm trying to keep the state of. Currently I have it where the user can edit inline( from within the gridview). Regularly I got this working:
protected void GridViewTower_RowEditing(object sender, GridViewEditEventArgs e)
{
//Set the edit index.
GridViewTower.EditIndex = e.NewEditIndex;
//Bind/Re-LoadData data to the GridView control.
LoadData();
Populate();
}
protected void GridViewTower_CancelEditRow(object sender, GridViewCancelEditEventArgs e)
{
//Reset the edit index.
GridViewTower.EditIndex = -1;
//Bind/Re-LoadData data to the GridView control.
LoadData();
Populate();
}
Problem is, I have 3 other features such sorting, dropdown that filters gridview, and a button search which also filters the girdview. When inline editing within any 3 of those modes, I can't control the state in which the gridview is in. Inside my gridview tag, I have both EnableViewState and ViewStateMode set to true.
How can I keep the state of the gridview within these modes?
public void LoadData()
{
if (Session["GridView"] != null)
{
GridViewTower.DataSource = Session["GridView"];
GridViewTower.DataBind();
//Response.Redirect("TowerManagement.aspx"); //
//Session["GridView"] = null;
}
else
{
WISSModel.WISSEntities context = new WISSModel.WISSEntities();
var tower = (from t in context.Towers
where t.isDeleted == false
select new
{
t.TowerId,
t.TowerName,
RangeName = t.Range.RangeName
}).ToList();
GridViewTower.DataSource = tower;
GridViewTower.DataBind();
ViewState["Sort"] = 0;
}
}
protected void Gridview_Sort(object sender, GridViewSortEventArgs e)
{
WISSModel.WISSEntities context = new WISSModel.WISSEntities();
var towers = (from t in context.Towers
where t.isDeleted == false
select new
{
t.TowerId,
t.TowerName,
rangeName = t.Range.RangeName
}).ToList();
DataTable gridviewTable = towers.CopyToDataTable();
gridviewTable.DefaultView.Sort = e.SortExpression + " " + ConvertSortDirectionToSql(e.SortDirection);
GridViewTower.DataSource = gridviewTable;
GridViewTower.DataBind();
Session["GridView"] = GridViewTower.DataSource;
}
You don't need to store whole table in the Session or ViewState. Just store values of SortExpression, SortOrder etc. Here's an example how you can do it.
In my code I have added two private properties to store sortorder and sortexpression:
private string SortOrder
{
get
{
// Toggle order after sorting
string _order = "ASC";//Default
if( ViewState["SortOrder"] != null && ViewState["SortOrder"].ToString() =="DESC")
{
_order = "DESC";
ViewState["SortOrder"] = "ASC";
}
else
{
ViewState["SortOrder"] = "DESC";
}
return _order;
}
set
{
string _order = value.ToLower() == "descending"? "DESC" : "ASC";
ViewState["SortOrder"] = _order;
}
}
private string SortExpression
{
get
{
return ViewState["SortExpression"] != null ? ViewState["SortExpression"].ToString() : "";
}
set
{
ViewState["SortExpression"] = value;
}
}
I have changed your GridView_Sort method to store the sort expression and sort order in newly added properties and called LoadData() method:
protected void Gridview_Sort(object sender, GridViewSortEventArgs e)
{
SortExpression = e.SortExpression;
//Disabled sort direction to enable toggling
//SortOrder = e.SortDirection.ToString();
LoadData();
}
The LoadData() method will be called from many places, whenever we want to load data into GridView. So I have changed it to this:
public void LoadData()
{
WISSModel.WISSEntities context = new WISSModel.WISSEntities();
var towers = (from t in context.Towers
where t.isDeleted == false
select new
{
t.TowerId,
t.TowerName,
rangeName = t.Range.RangeName
}).ToList();
DataTable gridviewTable = new DataTable();
gridviewTable.Columns.Add("TowerId");
gridviewTable.Columns.Add("TowerName");
gridviewTable.Columns.Add("rangeName");
foreach (var t in towers)
{
gridviewTable.Rows.Add(new object[] { t.TowerId, t.TowerName, t.rangeName });
}
if (!String.IsNullOrEmpty(SortExpression))
{
gridviewTable.DefaultView.Sort = String.Format("{0} {1}", SortExpression, SortOrder);
gridviewTable = gridviewTable.DefaultView.ToTable();
}
GridViewTower.DataSource = gridviewTable;
GridViewTower.DataBind();
}
Initially I call the LoadData() method in Page_Load() :
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
LoadData();
}
}
You can download the test project here.
I have a simple task. first one Dropdownlist control is there where country name is loaded. After selecting country name, dynamically Dropdownlist will be loaded with corresponding state, after selecting state, dynamically another Dropdownlist will be added with relevant district. the problem is that the dynamically selected-index event is not fired. I searched it so many pages, but not find any suitable answer. can any one answer it for written code.
This code worked fine in static controls. but not dynamic controls.
Can any one correct my code
namespace fireProgram
{
public partial class MindforeSystemTestingProgram : System.Web.UI.Page
{
BALayer objBALayer = new BALayer();
DropDownList ddlState=new DropDownList();
DropDownList ddlDistrict=new DropDownList();
protected void Page_Init(EventArgs e)
{
ddlState.ID = "ddlState";
ddlState.AutoPostBack = true;
ddlState.SelectedIndexChanged += new EventHandler(ddlState_SelectedIndexChanged);
panel1.Controls.AddAt(2, ddlState);
ddlDistrict.ID = "ddlDistrict";
panel1.Controls.AddAt(3, ddlDistrict);
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ddlCountry.DataSource = objBALayer.GetCountry();
ddlCountry.DataTextField = "Text";
ddlCountry.DataValueField = "Value";
ddlCountry.DataBind();
}
//else
//{
// ddlState.ID = "ddlState";
// ddlState.AutoPostBack = true;
// ddlState.SelectedIndexChanged += new EventHandler(ddlState_SelectedIndexChanged);
// panel1.Controls.AddAt(2, ddlState);
// //ddlDistrict.ID = "ddlDistrict";
// //panel1.Controls.AddAt(3, ddlDistrict);
//}
}
protected void ddlCountry_SelectedIndexChanged(object sender, EventArgs e)
{
int value = Convert.ToInt32(ddlCountry.SelectedValue);
panel1.Controls.AddAt(2, ddlState);
//DropDownList ddlState = new DropDownList();
//ddlState.AutoPostBack = true;
if (value != 0)
{
ddlState.DataSource = objBALayer.GetState(value);
ddlState.DataTextField = "Text";
ddlState.DataValueField = "Value";
ddlState.DataBind();
}
}
protected void ddlState_SelectedIndexChanged(object sender, EventArgs e)
{
int value = Convert.ToInt32(ddlState.SelectedValue);
//DropDownList ddlDistrict = new DropDownList()
panel1.Controls.AddAt(3, ddlDistrict);
if (value != 0)
{
ddlDistrict.DataSource = objBALayer.GetDistrict(value);
ddlDistrict.DataTextField = "Text";
ddlDistrict.DataValueField = "Value";
ddlDistrict.DataBind();
}
}
}
}
I've got dynamically created GridView in code behind. Now during actual DataBinding event I've got a JavaScript function in place that is saving edited cell and its value. This all works fine without a pager, now when I add pager to GW I need to add to JS function the Page Index so I can calculate later which row/cell it actually was that got changed.
// Databind an edit box in the grid
void edt_DataBinding(object sender, EventArgs e)
{
DropDownList txtData = (DropDownList)sender;
GridViewRow container = (GridViewRow)txtData.NamingContainer;
object dataValue = DataBinder.Eval(container.DataItem, _columnName);
txtData.Attributes.Add("onchange", "sav(" + container.RowIndex.ToString() + "," + _columnName + ",this.value)");
if (dataValue != DBNull.Value)
{
txtData.SelectedItem.Text = dataValue.ToString();
txtData.BackColor = Color.LightGreen;
}
}
I tried to add "grdMain.PageIndex" to the JS but the following error occurs: "Cannot access the non-static member of outer type....
txtData.Attributes.Add("onchange", "sav(" + grdMain.PageIndex + container.RowIndex.ToString() + "," + _columnName + ",this.value)");
Any intelligent workaround?
More code:
public class GridViewTemplate : ITemplate
{
private ListItemType _templateType;
private string _columnName;
private string _col;
private string _dataType;
private bool _isLabel;
private bool _isCategory;
private string _types;
private IQueryable _role;
public GridViewTemplate(ListItemType type, string colname, string col, string DataType, bool isLabel, bool isCategory, string types, IQueryable role)
{
_templateType = type;
_columnName = colname;
_dataType = DataType;
_col = col;
_isLabel = isLabel;
_isCategory = isCategory;
_types = types;
_role = role;
}
void ITemplate.InstantiateIn(System.Web.UI.Control container)
{
Label lbl = new Label();
switch (_types)
{....
}
}
void lbl_DataBinding(object sender, EventArgs e)
{
Label lbl = (Label)sender;
GridViewRow container = (GridViewRow)lbl.NamingContainer;
object dataValue = DataBinder.Eval(container.DataItem, _columnName);
if (dataValue != DBNull.Value)
{
lbl.Text = dataValue.ToString();
}
}
// Databind an edit box in the grid
void edt_DataBinding(object sender, EventArgs e)
{....
}
}
Button Click:
.....
TemplateField tfUsers = new TemplateField();
grdMain.Columns.Add(tfUsers);
tfUsers.ItemTemplate = new GridViewTemplate(ListItemType.Item, "Name", "0", "String", true, false, "resource", lame);
tfUsers.HeaderTemplate = new GridViewTemplate(ListItemType.Header, "Resource", "0", "String", true, false, "resource", lame);
_dtEntry.Columns.Add("Name");
......
i dont know your full code so i am guessing here...
have you tried in ItemDataBound?
something like this...
protected void grdMain_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
txtData.Attributes.Add("onchange", "sav(" + grdMain.PageIndex + container.RowIndex.ToString() + "," + _columnName + ",this.value)");
}
}