Access to a GridCheckboxColumn - c#

I have a GridCheckboxColumn in my grid and a need to create a GridTemplateColumn according to the checkbox is checked or not.
For instance,
line 1,
if the checkbox is checked the GridTempleColumn label displays "YES" ,
if the checkbox is not checked the GridTempleColumn label displays "NO",
if the checkbox is NULL : it displays "N/A".
To sum up, i have this in my aspx page:
<telerik:GridCheckBoxColumn DataField="facturable" DataType="System.Boolean" HeaderText="facturable"
SortExpression="facturable" UniqueName="facturable">
</telerik:GridCheckBoxColumn>
<telerik:GridTemplateColumn HeaderText="Type de tickets"
UniqueName="typedestickets">
<ItemTemplate><asp:Label id="test" runat="server"></asp:Label></ItemTemplate>
</telerik:GridTemplateColumn>
My try in code behind : ( not working )
protected void RadGrid1_DataBound(object sender, EventArgs e)
{
foreach (Telerik.Web.UI.GridDataItem dataItem in RadGrid1.MasterTableView.Items)
{
CheckBox chkDelete = (CheckBox)dataItem.FindControl("facturable");
Label label = (Label)dataItem.FindControl("test");
if (chkDelete.Checked == true) { label.Text = "MA"; }
}
}
Thank you in advance for your help

Check "e.item" is GridDataItem before accessing your controls. I perform a similar check in an application I'm working on, except I'm using "OnItemDatabound".
protected void grdSummary_OnItemDataBound(object sender, GridItemEventArgs e)
{
if (e.Item is GridDataItem)
{
// Access your controls here
}
}

CheckBox checkboxfacturable = (CheckBox)dataItem["facturable"].Controls[0];
Label label = (Label)dataItem["typedestickets"].Controls[0];

Most controls, including grids, can bind a variety of objects, not just your data items. You should always check that e.Item is of the correct type first thing in your OnItemDataBound handler before using it.

Related

How do you get POSTed value from dynamically populated dropdownlist without the use of UpdatePanel?

I have a Web Form dependent on a Master Page, that contains a simple Form like this:
<asp:TextBox runat="server" ID="txtValue"></asp:TextBox>
<asp:DropDownList runat="server" ID="ddlProduct" />
<asp:Button runat="server" ID="btnConfirm" Text="Confirm" OnClick="btnConfirm_OnClick" />
on Page_Load I dynamically populate the DropDownList with values:
foreach(var product in products)
{
ListItem item = new ListItem(product.Nazev, product.ProductId.ToString());
ddlProduct.Items.Add(item);
}
Now .. normally if the DropDownList was populated statically, I would go with this, to get my selected value out of it:
protected void btnConfirm_OnClick(object sender, EventArgs e)
{
string selectedProduct = ddlProduct.SelectedValue;
}
But this is a no-go in this situation. Hence I try to directly get the selected value from POSTed parameters with one of those approaches (which are in fact practically the same):
protected void btnConfirm_OnClick(object sender, EventArgs e)
{
string selectedProduct = Request.Form.Get(ddlProdukt.ClientID);
string selectedProduct2 = Request.Params[ddlProdukt.ClientID];
}
But it doesn't work. If I debug it, the actual "id/name" of the DropDownList in the POSTed Form (Request.Params) is:
"ctl00$MainContent$ddlProdukt"
whereas the ClientId my approach gives me is:
"ctl00_MainContent_ddlProdukt"
I can't figure out, why does it replace '_' for '$' in the ID of my DDL control.. It seems to be such a trivial thing. There should be a better way to find out the right ID, than replacing the character, right?
Is there some other way I should "look/ask" for the selected value?
*Take into account, that I'am not looking for a solution with an UpdatePanel. I know, that using an UpdatePanel would result in the DDL value being sucessfully selected after the Postback, but that is NOT the answer I'm looking for here.
Thank you for any input.
If you still need to use the Request.Form.Get or Request.Params you should call it on ddlProdukt.UniqueID as it's separator is '$' versus '_' on the .ClientID
As requested an example. This code and the <asp:DropDownList ID="ddlProduct" runat="server"></asp:DropDownList> are on the aspx page, not the Master.
protected void Page_Load(object sender, EventArgs e)
{
//bind data not here
if (IsPostBack == false)
{
//but inside the ispostback check
ddlProduct.Items.Add(new ListItem("Item 1", "1"));
ddlProduct.Items.Add(new ListItem("Item 2", "2"));
ddlProduct.Items.Add(new ListItem("Item 3", "3"));
ddlProduct.DataBind();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
Label1.Text = ddlProduct.SelectedValue;
}

Hide DataList row based on value from web service

I am retrieving a set of value from a web service and populating a dataList with those values-
ActiveDataList.DataSource = ws.TermsReturnActive(sql);
ActiveDataList.DataBind();
How can I hide a particular column of the dataList depending on the value,
e.g.
if(value == 1)
{
//Hide Column
}
This action however would have to hide the same row of another dataList in parallel to it.
I can modify a cell on this second dataList using by retrieving the value from the first as so -
TextBox tb1 = (TextBox)sender;
DataListItem item1 = (DataListItem)tb1.NamingContainer;
TextBox txt1 = (TextBox)tData.Items[item1.ItemIndex].FindControl("tTextBox");
string term = txt1.Text;
So if I can retrieve a value from a separate dataList row, I was thinking I would also be able to adjust its visability.
How can I achieve this as the web service call is done in the page load so I believe it would have to be done when the dataList item is bound?
if iam right, you should have something like this in your aspx-file right?:
<asp:DataList ID="DataList1" runat="server" OnItemDataBound="DataList1_ItemDataBound"></asp:DataList>
As you can see you should add a "OnItemDataBound" Event where you can check your value and hide a item, if you want.
So you can react like this and hide some items:
protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item)
{
TextBox tbCurrentTextBox = (TextBox)e.Item.FindControl("tTextBox");
if (DataList1.Items[e.Item.ItemIndex].ToString() == "1")
{
e.Item.Visible = false;
}
}
}

get value on edititemtemplate from codebehind

I have a detailsview where I get couple of data from membership profile and I display it on detailsview...this works fine:
<ItemTemplate>
<asp:label ID="FirstName" runat="server" />
</ItemTemplate>
But when I click the edit button, nothing shows up on the field. This is what I am doing on Edit template:
I call ItemUpdating like this:
protected void DetailsView1_ItemUpdating(Object sender, DetailsViewUpdateEventArgs e)
{
//I get my memberprofle here
MemberProfile memberp = MemberProfile.GetuserProfile(data);
MembershipUser myuser = Membership.GetUser()
Label labelfName = DetailsView1.FindControl("FirstName") as Label;
labelfName.Text = memberp.fName;
}
Should I be using Itemupdated instead? Or is there another method that I should call when the edit button is clicked that will populate the firstname field on edit? Also, the reason I am keeping it as "LABEL"(usually it would be textbox) on edit mode is that this field has to be read only.
The event ItemUpdating is not fired when you enter on edit mode. You must use DataBound event to set properly required text value.
If neccesary, you can ask CurrentMode property of DetailsView to know if you are editing or displaying.
The result looks like this:
protected void DetailsView1_DataBound(object sender, EventArgs e)
{
Label l = DetailsView1.FindControl("FirstName") as Label;
if (DetailsView1.CurrentMode == DetailsViewMode.Edit)
{
//obtained from your sample
MemberProfile memberp = MemberProfile.GetuserProfile(data);
MembershipUser myuser = Membership.GetUser()
l.Text = memberp.fName;
}
else
{
l.Text = "Another text or nothing";
}
}
Be sure to define DataBound event in you Detailsview1 control.
REMARK: It can be affected depending the data bind mode. If so, let me know and put an example.
Add RowUpdating and RowEditing event to your gridview.
http://www.aspdotnet-suresh.com/2011/02/how-to-inserteditupdate-and-delete-data.html

HtmlInputCheckBox in Repeater always not checked

i'm using a HtmlInputCheckBox in a repeater by adding
<input id="CheckBox1" type="checkbox" runat="server" value='<%# Eval ("userid") %>' />
to repeater->ItemTemplate->table->tr->td and in the server side i'm using
protected void Button1_Click(object sender, EventArgs e)
{
for (int i = 0; i < UserRepeater.Items.Count; i++)
{
var chkBox = UserRepeater.Items[i].FindControl("CheckBox1") as HtmlInputCheckBox;
if (chkBox != null && chkBox.Checked)
{
//
}
}
}
i'm not programatically setting any checkbox to set - i'm checking them on the web page during test.
my var checkbox is always inchecked {Value = "1,2,3,4" Checked = false}, thx for helping me with that.
How are you populating your repeater - if you are doing it in page_load make sure it is protected for postbacks:
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
// populate your data
}
}
EDIT
This is assuming you are working with viewstate on - which is the case by default.
This may be to do with when you bind your repeater. If you are binding on Page_Load, the check boxes will be created after viewstate and post variables have been restored, so the value won't be on your checkboxes.
If possible, move the data bind to Page_Init; as this happens before viewstate/post values are restored your checkboxes will get the right values assigned. If you can't bind on Page_Init, then #Aristos's answer will do.

Obtaining the ctl property from a gridview control in C#, ASP.Net

I am trying to obtain the ctl property that is specific to each row in a gridview. In my scenario, there is a list of titles, each title is displayed as a hyperlink using the LinkButton. When this is clicked, I would like to pass the ctl property (or what ever value is specific to that title, to the database and pull the information that is relative to the value).
I guess my first step is to obtain the ctl value. Could someone please help me get on my way. Thanks in advance.
You will need to keep track of the control ID that is written to the page in the row created event. Make a integer to increment for each row written and save it as a command argument on a link in the grid.
linkbutton CommandArgument='<%# Eval("some_id") %>'
protected void linkButton_Click(object sender, EventArgs e)
{
LinkButton linkButton = (LinkButton) sender;
if (linkButton != null)
{
if (linkButton.CommandArgument != null)
{
...some code...
}
}
}

Categories

Resources