accessing control/Checkbox in editform - asp.net

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");

Related

ID of items in ASP.NET ListView is automatically extended

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?

Using the FooTable plugin with the ASP.NET DataGrid

I am currently redesigning an application for the company I work for. All the data is displayed through a DataGrid, which is fine, except for when I try to incorporate a JQuery library called "Footable" to make all the grids collapse when on a smaller device as explained here. In the tutorial it uses a GridView. I made the demo, it worked well and it's exactly what I want, but I'm having trouble doing the same thing with a DataGrid. My first idea was to switch my DataGrid for a Gridview and implement it as in the demo, but I'd have to rewire everything in the code behind, which is vast, and would be very labour intensive. So I was wondering if it's possible to implement the FooTable JQuery plugin with a DataGrid instead of the GridView
There are only a couple of lines in the code behind that actually trigger the JQuery, but it doesn't seem to work on a DataGrid. Here is what the demo does with the GridView (in C#):
//Attribute to show the Plus Minus Button.
GridView1.HeaderRow.Cells[0].Attributes["data-class"] = "expand";
//Attribute to hide column in Phone.
GridView1.HeaderRow.Cells[2].Attributes["data-hide"] = "phone";
GridView1.HeaderRow.Cells[3].Attributes["data-hide"] = "phone";
//Adds THEAD and TBODY to GridView.
GridView1.HeaderRow.TableSection = TableRowSection.TableHeader;
Here is my attempt at it through datagrid (in VB.NET), but no success:
'Attribute to show the Plus Minus Button.
e.Item.Cells(0).Attributes("data-class") = "expand"
'Attribute to hide column in Phone.
e.Item.Cells(2).Attributes("data-hide") = "phone"
'Adds THEAD and TBODY to GridView.
e.Item.TableSection = TableRowSection.TableHeader
From what I have been reading a GridView is basically a super DataGrid with extra attributes, so I'm skeptic if I can actually implement the FooTable plugin with a DataGrid. Any help would be great, I'm not expecting anybody to hold my hand through this, just a point in the right direction.
Even if it's harder to implement the FooTable plugin with the DataGrid than with a GridView, it does not means it's not possible. If you read a little bit the FooTable introduction page, you realize that this plugin works with the HTML table element. So, no matter the control you are using with whatever technology, as long as it generates a table, it should work.
The code proposed in the above example which works with the Gridview control, renders the following table:
<table class="footable" cellspacing="0" rules="all" border="1" id="MainContent_GridView1" style="border-collapse:collapse;max-width: 500px">
<thead>
<tr>
<th data-class="expand" scope="col">Customer Name</th><th scope="col">Customer Id</th><th data-hide="phone" scope="col">Country</th><th data-hide="phone" scope="col">Salary</th>
</tr>
</thead>
<tbody>
<tr>
<td>John Hammond</td><td>1</td><td>United States</td><td>70000</td>
</tr>
<tr>
<td>Mudassar Khan</td><td>2</td><td>India</td><td>40000</td>
</tr>
<tr>
<td>Suzanne Mathews</td><td>3</td><td>France</td><td>30000</td>
</tr>
<tr>
<td>Robert Schidner</td><td>4</td><td>Russia</td><td>50000</td>
</tr>
</tbody>
</table>
So let's try to render the same code with the DataGrid. It should be similar to this in your aspx page:
<asp:DataGrid runat="server" ID="dataGrid1" ShowHeader="true" AutoGenerateColumns="false" UseAccessibleHeader="true">
<Columns>
<asp:BoundColumn DataField="Name" HeaderText="Customer Name" />
<asp:BoundColumn DataField="Id" HeaderText="Customer Id" />
<asp:BoundColumn DataField="Country" HeaderText="Country" />
<asp:BoundColumn DataField="Salary" HeaderText="Salary" />
</Columns>
</asp:DataGrid>
A DataGrid must have asp:BoundColumns instead of asp:BoundFields (in the GridView). Also, be sure that ShowHeader="true", other else the DataGrid won't generate your header. UseAccessibleHeader="true" is important as well, because without it, your header cells will be generated as tr instead of th.
The principal difficulty in implementing the header attributes (e.g. data-class= "expand") is accessing your header. But it is feasible. In your code behind, in the Page_Load event, add the following code (after the DataGrid's DataBind):
' That's how you can access to your header
Dim dataGridHeader As DataGridItem = dataGrid1.Controls(0).Controls(0)
' I fyou need to access to your footer, do the following:
' MyGrid.Controls(0).Controls(MyGrid.Controls(0).Con trols.Count - 1)
dataGridHeader.Cells(0).Attributes("data-class") = "expand"
'Attribute to hide column in Phone.
dataGridHeader.Cells(2).Attributes("data-hide") = "phone"
dataGridHeader.Cells(3).Attributes("data-hide") = "phone"
' Make the header row part of a `thead` instead of `tbody` (by default)
dataGridHeader.TableSection = TableRowSection.TableHeader
If you followed all the instructions, you should get the same HTML Table as the one specified earlier. Keep in mind that no matter what you're trying to do with the FooTable plugin, it's all as possible with the DataGrid as it is with the GridView, since both generate an HTML Table, the element aimed by the plugin.

asp.net listview how to fire itemcommand event on a <tr>?

and thanks for reading my post.
I think.. this might be pretty easy for a experienced asp.net developer which have tweaked much with listviews and such.
The thing is.. I have this LinkButton which ofcourse.. have the commandname and commandargument and that works like a charm!
The issue is.. that the customer wants the WHOLE row to be clickable.. not just the linkbutton in the first .
So.. therefore.. i'm trying to force a itemcommand on a table row (tr)
here is the code :
<asp:ListView runat="server" ID="lvProdukter" DataKeyNames="ProduktSXX" OnItemCommand="lvProdukter_ItemCommand" ItemType="ProductXXXX">
<ItemTemplate>
<tr>
<td>
<asp:LinkButton runat="server" ID="lnkSelectProdukt" CssClass="lvitemlink" CommandArgument='<%# Item.ProduktS + ";" + Item.VaregrId %>'
CommandName="SelectProdukt">
<%# Item.TheName ?? " " %>
</asp:LinkButton>
</td>
<td>
bla-bla-bla..
</td>
etc.etc....
Well I haven't tried it but I think there are two ways of doing this.
Make the tr runat='server' and on click use _dopostback function. and see it works or not.
Use jQuery tr click and call link button click inside it.
$("#lvProdukter tr").click(function()
{
//call the link button click here.
});
use Jquery on the client side:
$('#table-name').on('click', 'tr', function()
{
alert('Hello');
});

FormView not passing a value contained within "runat=server" row

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;
}
}

How to dynamically assign datasource to listview

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?

Resources