I have the following code in the EditItemTemplate of my FormView:
<tr id="primaryGroupRow" runat="server">
<td class="Fieldname">Primary Group:</td>
<td><asp:DropDownList ID="iPrimaryGroupDropDownList" runat="server" DataSourceID="GroupDataSource" CssClass="PageText"
DataTextField="sGroupName" DataValueField="iGroupID" SelectedValue='<%# Bind("iPrimaryGroup") %>'></asp:DropDownList></td>
</tr>
If I remove the runat="server" for the table row, then the iPrimaryGroup field is bound 100% and passed to the business logic layer properly. However in the case of the code above, it is passed with a value of zero.
Can anyone tell me why this is or how to get around it? This is in a control that needs to hide this table row, based on whether or not an administrator or a regular user is editing it. ie: some fields are admin writeable only and I'd like to hide the controls from the view if the user isn't an admin.
If security is a concern perhaps this might work better
<tr>
<td colspan='2'>
<asp:panel runat='server' visible='<%= IsUserAdmin %>'>
<table>
<tr>
<td class="Fieldname">Primary Group:</td>
<td><asp:DropDownList ID="iPrimaryGroupDropDownList" runat="server" DataSourceID="GroupDataSource" CssClass="PageText" DataTextField="sGroupName" DataValueField="iGroupID" SelectedValue='<%# Bind("iPrimaryGroup") %>'></asp:DropDownList>
</td>
</tr>
</table>
</asp:panel>
</td>
If I'm not mistaken any markup within the panel will not be rendered if visible=false
Have a shot at this:
Remove the runat=server attribute
Define a css class
.hidden{ display:hidden;}
Then set the class attribute based on whether or not the user is an admin
<tr class='<%= if(IsUserAdmin) "" else "hidden" %>' >
It appears that this functionality is by design, although that's not exactly confirmed.
http://weblogs.asp.net/rajbk/archive/2009/08/03/formview-binding-gotcha.aspx
When using the FormView object, if you have a nested control, then two-way databinding isn't going to work properly. You can access the controls in code, and you can get at the data, but it's just not going to automatically update the value in the back end of your Business Logic Layer(BLL) like it's supposed to.
Fortunately, there's a workaround. The way to get it working is to create an event for ItemUpdating. It will have a signature like this:
protected void frmProfile_ItemUpdating(object sender, FormViewUpdateEventArgs e)
This gives you access to the FormViewUpdateEventArgs, which in turn allows you to make changes to the ObjectDataSource values while they are in flight and before they hit your BLL code, as follows:
protected void frmProfile_ItemUpdating(object sender, FormViewUpdateEventArgs e)
{
if (frmProfile.FindControl("iPrimaryGroupDropDownList") != null)
{
DropDownList iPrimaryGroupDropDownList = ((DropDownList)frmProfile.FindControl("iPrimaryGroupDropDownList"));
e.NewValues["iPrimaryGroup"] = iPrimaryGroupDropDownList.Text;
}
}
Related
I have programmed a simple blog page - http://www.3don.net.br/Blog.aspx (another language, here only to show the structure). I want to use hashtags for pointing to the topics. For example, http://www.3don.net.br/Blog.aspx#19/04/16 should scroll the page to the topic created at 19/04/16.
However, I cannot get it!
The topics of the blog are ItemTemplates of a ListView control. When I define an ID="lblDatum" for the label control of the data of each topic (which is the first control of each topic), then this ID is modified by the NET machine to
id="ctl00_ContentPlaceHolder_lstBlog_ctrl0_ctl01_lblDatum" (you can see it in the source code of the page for the second topic, for example).
So, if I access in the browser www.3don.net.br/Blog.aspx#ctl00_ContentPlaceHolder_lstBlog_ctrl0_ctl01_lblDatum
the page indeed will scroll correctly. I can also programmatically change the ID for each topic differently and it still works for each topic.
However, the hashtag-name "ctl00_ContentPlaceHolder_lstBlog_ctrl0_ctl01_lblDatum" is not nice! Is there a possibility to suppress the ctl00_ContentPlaceHolder_lstBlog_ctrl0_ctl01_-part?
Or another idea for getting it?
Use ClientIdMode="Static" in your ListItem, which removes the autoGenerate ID, you got ID="lblDatum" on client.
ok, I added the clientIDMode="static" property to the Label control of the data:
<table id="table_intern" runat="server" >
<tr id="tr_intern" runat="server">
<td id="td_intern" runat="server">
<asp:Label ID="lblDatum" runat="server" Text='<%# Eval("data") %>' CssClass="rotfettschrift" clientidmode="static"/>
</td>
</tr>
<tr> ... </tr>
<tr> ... </tr>
</table>
... and, in code-behind, set the ID of this control of each topic igual of the text property of this control (the data):
lbl = CType(e.Item.FindControl("lblDatum"), Label)
lbl.ID = lbl.Text
However, the HTML-output is:
<tbody>
<tr id="ctl00_ContentPlaceHolder_lstBlog_ctrl0_ctl00_tr_intern">
<td id="ctl00_ContentPlaceHolder_lstBlog_ctrl0_ctl00_td_intern">
<span id="ctl00_ContentPlaceHolder_lstBlog_ctrl0_ctl00_19/05/16" class="rotfettschrift" clientidmode="static">19/05/16</span>
</td>
</tr>
<tr> ... </tr>
<tr> ... </tr>
</tbody>
Why is the ID of the span element ctl00_ContentPlaceHolder_lstBlog_ctrl0_ctl00_19/05/16 and not just 19/05/16 ???
Ok, i tested here, using reapeater, but with any other control works. You must set the id individually to each control, to make unique.
*I redid, using date.
MARKUP:
<table id="table_intern">
<asp:Repeater runat="server" ID="repeater" OnItemDataBound="repeater_ItemDataBound">
<ItemTemplate>
<tr>
<td>
<asp:Label Text="" runat="server" ID="label" />
</td>
</tr>
</ItemTemplate>
</asp:Repeater>
</table>
BACKEND:
protected void Page_Load(object sender, EventArgs e)
{
var list = new List<string>();
for (int i = 0; i < 10; i++)
{
list.Add(string.Format("Item_{0}", i.ToString().PadLeft(2, '0')));
}
repeater.DataSource = list;
repeater.DataBind();
}
protected void repeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
var label = (Label)e.Item.FindControl("label");
var item = (DateTime)e.Item.DataItem;
label.ID = item.ToShortDateString();
label.Text = item.ToShortDateString();
label.ClientIDMode = ClientIDMode.Static;
}
}
RESULT:
<table id="table_intern">
<tbody><tr>
<td>
<span id="23/05/2016">23/05/2016</span>
</td>
</tr>
<tr>
<td>
<span id="24/05/2016">24/05/2016</span>
</td>
</tr>...
Sorry, for bad formatting, i will improve my answers if necessary.
Daniel,
thank you for dedicating your time.
You are defining the ClientIDMode property of the Label control in the code behind, ok. I do so, but I see that it does not work here:
screenshot
When I go with the mouse pointer over the ClientIDMode at the left it advices: "'ClientIDMode' is not a member of 'Label'"
and over the ClientIDMode at the right, it pops "'ClientIDMode' is not declared. It may be inaccessible due to its protection level."
Is there something undefined in my system?
Ok, i understand you using Framework2.0 in your project. Can you change Framework to 4.0 or upper? Otherwise you need use a .js approach, to scroll like wish.
ClientIdMode is from .net 4.0, how you have said .net4.6 installed, I assumed that the project was also 4.6.
IF, you can't change the version, you need use something like jquery to scroll. OR maybe a more hard solution creating a item in your itemTemplate like:
<span id='<%# Eval("data") %>'></span>
I don't have tested this solution, later I'll be testing;
Daniel,
I always suspected that, but the
"About Visual Studio" window tells another story.
Adicionally, in the registry I can find the v4/Full key and the Version information (at the right side) confirms the (installed and activated?) version 4.6.01055.
???
Or is framework v4 installed but not "activated"? Does this exist? Where can I see this?
I have a problem with accessing the Checkbox in my editForm-Template, that is nested in a Grid.
<EditFormSettings EditFormType="Template" EditColumn-UniqueName="insertForm">
<FormTemplate>
<table>
<tr>
<td>Add : </td>
<td>
<asp:CheckBox runat="server" ID="addCB" />
</td>
</tr>
<tr>
</table>
</FormTemplate>
</EditFormSettings>
The EditFormTemplate is like i said, nested in a Grid. I just didn`t post it here, to make it more clearly.
my problem is now that I neither can access the checkbox via javascript like :
function isAddSwitched() {
var checkbox = $find("<%= addCB.ClientID%>");
alert(checkbox.get_checked());
}
nor can i access it through code behind like this :
If addCB.Checked Then
'code here
End If
When I move the Checkbox out of the Template field its no problem at all. But INSIDE it, it seems impossible to access it.
It would be greatif someone could help me with that problem !!
Kind Regards,
Malte
This is because when controls are placed in a grid, they are not declared in the designer file as page controls.
You will have to get hold of them differently depending on what event is happening:
Control postback i.e. OnCheckedChanged: the sender object will be your checkbox
Row event e.g. OnItemDataBound, OnRowInsert, etc: you can get hold of the checkbox by using e.Item.FindControl("addCB");
I have a repeater in my aspx page. The datasource for this will change for different types of call hence I cannot do
<ItemTemplate>
<tr >
<td bgcolor="" valign="top">
<%#DataBinder.Eval(Container.DataItem, "Id")%>
</td>
...
as the dataItem will be different. Which is the best way to do it server side?
Thanks,
Personally, the best way will be create interface and write adapters for every type. In repeater you gonna be work only with this interface.
You can do
<asp:Label ID="lable1" runat="server" ondatabinding="label1_ondatabinding" />
and manually fill data in the event handler.
protected void label1_ondatabinding(object sender, EventArgs e)
{
((Label)sender).Text = ...
}
I have an ASP.NET page that has two input elements:
A TextBox that is ReadOnly. This TextBox is the TargetControl of a CalendarExtender
A DropDownList with AutoPostBack=true
Here is the code:
<table border="0" cellpadding="0" cellspacing="0">
<tr><td colspan="2">Date:</td></tr>
<tr><td colspan="2">
<asp:TextBox ID="dateTextBox" runat="server" ReadOnly="true" />
<ajax:CalendarExtender ID="datePicker" runat="server" Format="MM/dd/yyyy" OnLoad="datePicker_Load" TargetControlID="dateTextBox" />
</td></tr>
<tr><td colspan="2">Select an Option:</td></tr>
<tr>
<td>Name: </td>
<td><asp:DropDownList ID="optionsDropDownList" runat="server" AutoPostBack="true"
OnLoad="optionsDropDownList_Load"
OnSelectedIndexChanged="optionsDropDownList_SelectedIndexChanged"
DataTextField="Name" DataValueField="ID" />
</td></tr>
<tr><td><asp:Button ID="saveButton" runat="server" Text="Save" OnClick="saveButton_Click" /></td></tr>
</table>
When the DropDownList posts back, the date selected by the user with the datePicker is reset to the current date. In addition, if I look at the Text property of dateTextBox, it is equal to string.Empty.
How do I preserve the date that the user selected on a PostBack?
Certainly you must do as others have already suggested: set readonly field dynamically rather than in markup, and make sure you are not accidentally resetting the value in Page_Load() during postbacks...
...but you must also do the following inside Page_Load(), because the CalendarExtender object has an internal copy of the date that must be forcibly changed:
if (IsPostBack) // do this ONLY during postbacks
{
if (Request[txtDate.UniqueID] != null)
{
if (Request[txtDate.UniqueID].Length > 0)
{
txtDate.Text = Request[txtDate.UniqueID];
txtDateExtender.SelectedDate = DateTime.Parse(Request[txtDate.UniqueID]);
}
}
}
The fact that the text box is read only appears to be causing this problem. I duplicated your problem using no code in any of the bound events, and the date still disappeared. However, when I changed the text box to ReadOnly=False, it worked fine. Do you need to have the textbox be read only, or can you disable it or validate the date being entered?
EDIT: OK, I have an answer for you. According to this forum question, read only controls are not posted back to the server. So, when you do a postback you will lose the value in a read only control. You will need to not make the control read only.
protected void Page_Load(object sender, EventArgs e)
{
txt_sdate.Text = Request[txt_sdate.UniqueID];
}
If you want the textbox contents remembered after the postback and still keep it as readonly control, then you have to remove the readonly attribute from the markup, and add this in the codebehind pageload:
protected void Page_Load(object sender, EventArgs e){
TextBox1.Attributes.Add("readonly", "readonly");
// ...
}
The solution to the issue is making use of Request.Form collections. As this collection has values of all fields that are posted back to the server and also it has the values that are set using client side scripts like JavaScript.
Thus we need to do a small change in the way we are fetching the value server side.
C#
protected void Submit(object sender, EventArgs e)
{
string date = Request.Form[txtDate.UniqueID];
}
I suspect your datePicker_Load is setting something and not checking if it's in a postback. That would make it happen every time and look like it was 'resetting'.
In stead of setting ReadOnly="False", set Enabled="False". This should fix your issue.
#taeda's answer worked for me. FYI, if someone uses enabled = "false" instead of readonly = "true" wont be able to use that answer, because Request[TxtDate.UniqueId] will throw a null exception.
It is not a problem of Preserving,after setting readonly = true the value of the particular textbox doesn't be returned back to server
so
Use contentEditable="false" and made readonly = false
that would prevent value is being entered other than Calendar Extender selection also returns the value of the Text box back to the server
I am having a problem with dynamically assigning datasource to listview.
For example I have list of receivedBonuses(Bonus), receivedLeaves(Leave) and I want listview to display those list items depending on what link button user clicked.
Researching internet and stackoverflow.com i found 3 solutions:
Using repeater inside the listview. But in my case, I could not apply it to my case and i got totally confused
Using nested listviews. I tried to do like this:
<asp:ListView ID = "bonuses" runat="server" DataSource ='<%# Eval("received_bonuses") %>' >
<ItemTemplate>
<tr>
<td><%# Eval("bonus_desc")%></td>
<td><%# Eval("bonus_type")%></td>
</tr>
</ItemTemplate>
<LayoutTemplate>
<table>
<tr>
<th>Bonus Description</th>
<th>Bonus Received Date</th>
</tr>
<tr ID="itemPlaceholder" runat="server" />
</table>
</LayoutTemplate>
<table>
<tr>
<th>Bonus Description</th>
<th>Bonus Received Date</th>
</tr>
<tr ID="itemPlaceholder" runat="server" />
</table>
</LayoutTemplate>
</asp:ListView>
<br />
and on back code I tried to write like this:
protected void dataBound(object sender, ListViewItemEventArgs e)
{
this.DataBindChildren();
}
It didn't give any errors it just didn't work.
Using data pager
I have no idea how to apply it to my case.
Any help is appreciated.
Thanks a lot.
All you have to do on the server side is change the DataSource or DataSourceID property and call DataBind on the ListView.
You have to make sure when using <%# Eval("") %> syntax that the objects you are binding to have those properties that are named in the Eval. So you may have a problem with with switching datasources when your properties are prepended with the typename and underscore.
That being said. There are 2 options you have an changing a data source. In the click event of the button or whatever switching mechanism you are using you can just write something like.
Not using a DataSource in the markup:
List<Bonus> bonusList = GetBonuses();
MyListView.DataSource = bonusList;
MyListView.DataBind();
Using a DataSource in the markup:
//where bonus list would be the id of the datasource in the markup
MyListView.DataSourceID= "BonusList";
MyListView.DataBind();
Do you need to do this dynamically? If you only have "bonuse" and "leave" can you not create two listviews and then just do display logic to visible=true/false the listview based upon the link button clicked?