 |
|
 |
hi. your article is good enough to fullfill my requirments but i want this code in vb.net so that i can easly implement.
hope any of the readers will solve the problem for me in vb.net
be happy happy
|
|
|
|
 |
|
 |
same here..... plz help guys
|
|
|
|
 |
|
 |
There is no need to register the event in the ItemCreated. simply do the following:
1. <asp:TemplateColumn HeaderText="Checkbox">
<asp:CheckBox ID="cbExample" AutoPostBack="true" runat="server" OnCheckChanged="OnChangeHandler">
2. Make the OnChangeHandler event as protected or public.
This will work with no problems as I have done this in one of my projects.
Please let me know if there is any thing missing.
Sachin Bhatnagar.
|
|
|
|
 |
|
 |
You might want to rewrite that as the code is not showing up.
|
|
|
|
 |
|
 |
I get this error when i try to run the above sample.
CheckBox cBox = (CheckBox)e.Item.FindControl("CheckBox1");
Line 678: cBox.CheckedChanged += new EventHandler(this.OnChangeHandler);
Line 679:
Line 680: }
cBox is null.
how do i get the control?
|
|
|
|
 |
|
 |
Thanks to Mr.MKRUPPA, It's useful.
|
|
|
|
 |
|
 |
I have an asp.net page with a DataGrid like:
<asp:datagrid id="dgAddresses" runat="server" CssClass="grid" GridLines="None" AutoGenerateColumns="False">
I've created a TemplateColumn with a ImageButton:
<asp:TemplateColumn ItemStyle-VerticalAlign="Top">
<asp:ImageButton id="ibtnViewConnections" runat="server" CommandArgument='<%DataBinder.Eval(Container.DataItem, "lngAddressID").ToString()%>'>
in my .cs file I've added an
this.dgAddresses.ItemCreated += new System.Web.UI.WebControls.DataGridItemEventHandler(this.dgAddresses_ItemCreated);
This method is:
private void dgAddresses_ItemCreated(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
try
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
DataRowView drv = (DataRowView)e.Item.DataItem;
if(drv != null)
{
//Has loaded data from database
System.Web.UI.WebControls.ImageButton ibtn = (System.Web.UI.WebControls.ImageButton)e.Item.Cells[0].Controls[1];
ibtn.Click += new System.Web.UI.ImageClickEventHandler(this.ibtnViewConections_Click);
}
else
{
System.Web.UI.WebControls.ImageButton ibtn = null;
ibtn = (System.Web.UI.WebControls.ImageButton)e.Item.Cells[0].Controls[1];
if(ibtn.CommandArgument != "")
{
ibtn.Click += new System.Web.UI.ImageClickEventHandler(this.ibtnViewConections_Click);
.........
At first run the DataGrid draws itself perfect, with all it's values.
When I hit the ImageButton, it goes directly in to the dgAddresses_ItemCreated(..) methode, but the ImageButton has no values ( the CommandArgument is "") or in other words the
"System.Web.UI.WebControls.DataGridItemEventArgs e" is empty.
I've thougt it should contain the data posted from the clint side.
What am I doing wrong? And how can I catch the data?
In the same page I have a Button and a TextBox (Outside the DataGrid) . When I hit det Button it goes direct to the "Page_Load(object sender, System.EventArgs e)" method, at this point the TextBox.Text is empty. Next method it enters is the Button_Click(object sender, System.EventArgs e), at this point the TextBox.Text has got it's value(from clint side). This is ofcourse what I want, but how did this happen? In regards of the ImageButton where it didn't happen.
Thanks in advance for all help
Thomas
|
|
|
|
 |
|
 |
Sorry for my english
Im try to using this code with EditItemTemplate and never found child control
why????
carzel
|
|
|
|
 |
|
 |
Any Idea how to handle events for sub-controls of as usercontrol?
|
|
|
|
 |
|
 |
I have tried but the Intelisense of the radiobuttonlist doestn have the event selectedindexchanged? I have a datagrrid with an itemtemplate, inside this itemtemplate thre is a radiobuttonlist with 3 options. There is also a label to the side of this radiobuttonlist. When the user selects anyradiiobutton list the label must update with an informative message depending of what the user selected!!! <ItemTemplate> <asp:RadioButtonList id="seleccion" runat="server" Width="42px" RepeatDirection="Horizontal"> <asp:ListItem Value="0" Selected="True">0</asp:ListItem> <asp:ListItem Value="1">1</asp:ListItem> <asp:ListItem Value="2">2</asp:ListItem> <asp:ListItem Value="3">3</asp:ListItem> </asp:RadioButtonList> <asp:Label id="lblmeta" runat="server"></asp:Label> </ItemTemplate> On the itemcreated I tryed to follow the example Private Sub dgPreguntas_ItemCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles dgPreguntas.ItemCreated Dim elemtype As ListItemType = New ListItemType elemtype = e.Item.ItemType If elemtype = ListItemType.AlternatingItem And elemtype = ListItemType.Item Then Dim rdl As New RadioButtonList
rdl = CType(e.Item.FindControl("seleccion"), RadioButtonList) ' Here the intellisense doesnt down rdl.SelectedIndexchanged End If
End Sub Please Help me
|
|
|
|
 |
|
 |
OnCheckChanged="OnChangeHandler"
will work perfectly .
and won't make any mistake.
--------
this is all because of the code behind -
when you connect the handler it will create at compilation the delegate. and it will look like this:
chkbox1.OnCheckChanged += new OnCheckChanged
(OnChangeHandler);
and you could write your handler .
the only thing you should know is that this delgate is written at page.aspx class which inherit from the page.aspx.cs file .
so when you write your handler at the page.aspx.cs file you should give it the protected access modifier - because the page.aspx cannot access to private methods
(inheritance rules)
(for beginners - page.aspx inherit the code behind page.aspx.cs , and as we know child class cannot access his father private methods)
|
|
|
|
 |
|
 |
Thx! You are absolutly right. I'm so used to use VS.net for my work with windows forms that I didn't see the wood for the trees. Guess I now have to rewrite my article.
|
|
|
|
 |
|
 |
Sorry for my english
I need add new header to the datagrid for TITLE,
example:
| TITLE |
|TIT COLUMN 1 | TIT COLUMN 2 | TIT COLUMN 3 |
| item 1 | item 1 | item 1 |
| item 2 | item 2 | item 2 |
| item 3 | item 3 | item 3 |
I write the override OnPreRender for add new header, but the problem is when i click in order, paging or
editing column.
Error in viewstate?
The controls added in a devolution must match with type and position of the controls added in the initial request.
protected override void OnPreRender(EventArgs e)
{
DataGridItem dgitem = new DataGridItem (0, 0, ListItemType.Header) ;
TableCell tblCell = new TableCell();
tblCell.ColumnSpan = this.Columns.Count;
tblCell.Text = "Title";
dgitem.Cells.Add(tblCell);
this.Controls[0].Controls.AddAt (1, dgitem);
base.OnPreRender (e);
}
Can you help me?
Fernando Finelli
|
|
|
|
 |
|
 |
Hi, I have been looking for this code for quite a while and was very excited to see it. However, after doing the same thing as you, I still cant get the values. This is what I am doing: <code for aspx page>: <ItemTemplate> <asp:CheckBox id="CheckBox1" runat="server" AutoPostBack="True"></asp:CheckBox><BR> <ItemTemplate> </code> The code-behind:
<code>private void grid_ItemCreated( object sender, System.Web.UI.WebControls.DataGridItemEventArgs e) { ListItemType elemType = e.Item.ItemType; if ((elemType == ListItemType.Item)||(elemType == ListItemType.AlternatingItem)) { // find the checkbox inside the Grid CheckBox CheckBoxMappingTerritory = (CheckBox) e.Item.Cells[3].FindControl("CheckBox1"); // register event in the check box CheckBoxMappingTerritory.CheckedChanged += new EventHandler(OnChangeHandler); } } protected void OnChangeHandler(object sender, System.EventArgs e) { CheckBox CheckBoxMappingTerritory = (CheckBox)sender; DataGridItem GridItem = (DataGridItem)CheckBoxMappingTerritory.Parent.Parent; if (CheckBoxMappingTerritory.Checked == true) { Label2.Text = CheckBoxMappingTerritory.Checked.ToString(); } BindData(); }</code> So after the Grid has been rendered, I click on the checkbox and the grid dissappears. The Label.Text is not updated, so if you could shed some light i would really appreciate it. thank, shiv. shiv
|
|
|
|
 |
|
 |
Hi,
have you registered the ItemCreated eventhandler to the grid? Do you create the grid control by code or design time? What do you call the BindData() method for as you don't make any changes to underlying data and dont't change any data source? Are you absolutly sure that your CheckBox is in the forth column? You don't need the GridItem here, so comment it out. Why do you check if the CheckBox.Checked property is true if you just want the output of it's status, if it is false it dosn't matter. I usally recomend to reduce the code to the absolutly nessesary and use a debugger.
Have fun
Martin
|
|
|
|
 |
|
 |
Hi Martin:
Thanks for the quick response. I am creating the grid at design time. Yes I did register the ItemCreated eventhandler to the grid:
this.DataGrid1.ItemCreated += new System.Web.UI.WebControls.DataGridItemEventHandler(this.grid_ItemCreated);
Well as far as what column the checkbox is in - I have 5 columns and 1 of them is hidden, so it is either 4 or 5, depending on whether the column gets rendered, which in the case as I believe does nto get rendered. So it would be 4. I was checking for the checkbox value because i want to update a table column in my db if it is true. And I dont quite get why I wouldnt need the GidItem. How else will I know what row I am getting the checkbox value from?
See if you can add to what you said earlier after reading this, and if not then i will have to create a new project and use a debugger there (I cant use it on my current one since it breaks on some javascript..and i'll fix that later). And if you need me to show you some more code, let me know.
I really once again appreciate your help since this has been a issue pending for a while.
thanks,
shiv.
|
|
|
|
 |
|
 |
Hi Shiv,
the handle looks good.
Try finding the control without the column, like e.Item.FindControl("CheckBox1");
Sure you will need the GidItem later on, but comment it out if you don't need it now, just to ease your debugging.
Martin
|
|
|
|
 |
|
 |
Hey Martin,
I am sorry, your code does work. I tried on a separate page, and I can get the values. I guess the trouble I am facing is that everytime I add this code to the control that I use and everytime a user clicks on the checkbox, the grid disappears. I am not checking IsPostBack since the postback has already occured earlier, i.e. the rquest querystring for that control. Any ideas as to how I would take care of that? You might have to go back to the code I submitted. Thats the reason I was calling BindData() after the submit button and I thought i would need it for the CheckedChanged event too. I am sure I need to add just oen line, but I dont know where (my eyes are tired looking at the same code i guess).
And thanks a bunch for all the help so far.
Thanks,
Shiv.
shiv
|
|
|
|
 |
|
 |
Let me add to the previous email: What I am trying to do is present a datagrid where users can click on the checkboxes to say that those rows have been audited. And when they click on the checkbox, i want to update a column in a table and trying to acheive the fucntionality that the next time they come to that page, they can complete their selections to complete the audit process. Morelike allowing users to resume the audit process. I was trying to achieve this by having a "Save for later" button that would go thru the grid and grab all the rows that have been checked and update the table. So the next time they come to the same audit, they can resume where they ended. A final "Complete Audit" button would close that audit, update the db and flip a flag to say audit cimplete.However, in any scenairo, I am failing to get the checked value. I hope this helps to understand my situation. I am also including my design-time code for the grid. <code><asp:datagrid id="DataGrid1" runat="server" CssClass="BodyText" ShowHeader="False" Width="100%" AutoGenerateColumns="False"> <HeaderStyle Font-Size="Smaller" Font-Names="Arial" HorizontalAlign="Center" BackColor="Gray"></HeaderStyle> <Columns> <asp:BoundColumn DataField="c_Category" SortExpression="Category" HeaderText="Category"> <HeaderStyle Width="10%"></HeaderStyle> <ItemStyle Width="10%"></ItemStyle> </asp:BoundColumn> <asp:BoundColumn DataField="c_Sub_Category" SortExpression="SubCategory" HeaderText="Sub Category"> <HeaderStyle Width="10%"></HeaderStyle> <ItemStyle Width="10%"></ItemStyle> </asp:BoundColumn> <asp:BoundColumn Visible="False" DataField="c_WebNum"> <HeaderStyle Width="10%"></HeaderStyle> <ItemStyle Width="20%"></ItemStyle> </asp:BoundColumn> <asp:TemplateColumn HeaderText="Task"> <HeaderStyle Width="30%"></HeaderStyle> <ItemStyle Width="30%"></ItemStyle> <ItemTemplate> <asp:Label runat="server" ID="Label5" Text='<%# DataBinder.Eval(Container, "DataItem.c_Task") %>'> </asp:Label><br> </ItemTemplate> <EditItemTemplate> <asp:TextBox runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.c_Task") %>' > </asp:TextBox><br> </EditItemTemplate> </asp:TemplateColumn> <asp:TemplateColumn HeaderText="*TC"> <HeaderStyle Width="25%"></HeaderStyle> <ItemStyle Width="25%"></ItemStyle> <ItemTemplate> <asp:CheckBox id="CheckBox1" runat="server" AutoPostBack="True"></asp:CheckBox><BR> <asp:DropDownList id=ComplianceList runat="server" CssClass="BodyText" DataSource="<%# GetCompliance() %>" DataTextField="c_Desc" DataValueField="i_ColVal"> </asp:DropDownList><BR> <asp:TextBox id="Textbox5" runat="server" EnableViewState="False" Text=""></asp:TextBox> </ItemTemplate> </asp:TemplateColumn> <asp:TemplateColumn HeaderText="*DO"> <HeaderStyle Width="25%"></HeaderStyle> <ItemStyle Width="25%"></ItemStyle> <ItemTemplate> <INPUT id="shiv2" type="checkbox" runat="server"><BR> <asp:Label id="Label6" Runat="server">If not compliant,enter directions:</asp:Label><BR> <asp:TextBox id="Tbox1" runat="server" CssClass="BodyText" EnableViewState="False" Text="Compliant by 8/19/2004">Compliant by 8/19/2004</asp:TextBox><BR> </ItemTemplate> </asp:TemplateColumn> <asp:BoundColumn Visible="False"></asp:BoundColumn> </Columns> <PagerStyle VerticalAlign="Bottom" PageButtonCount="15" Mode="NumericPages"></PagerStyle> </asp:datagrid></code>
thanks, shiv.
|
|
|
|
 |
|
 |
Hi shiv,
may be you have also take a look at the BoundBooleanColumn of Andy Smith http://www.MetaBuilders.com [^]as it might suite your scenario a bit better.
But if you still like to handle the CheckChanged event directly, please let my have a look at your BindData() method and your other data handling procedures.
Martin
|
|
|
|
 |
|
 |
Hey Martin, I am looking at the BoundBooleanColumn. Meanwhile, I will include my BindData() method: My page load method: <code>private void Page_Load(object sender, System.EventArgs e) { GetCity(); }</code> <code>private void GetCity() { SqlConnection cn; cn = new SqlConnection("connection string"); cn.Open(); SqlDataReader dr = SqlHelper.ExecuteReader(cn,"dbo.GetCity"); CityList.Items.Insert(0,"Select from list..."); CityList.DataSource=dr; CityList.DataTextField="c_Office"; CityList.DataValueField="i_RecID"; CityList.DataBind(); CityList.Items.Insert(0, "Select from list..."); dr.Close(); cn.Close(); }</code> My BindData() method: <code>private void BindData() { SqlCommand cm; SqlConnection cn; cn = new SqlConnection("connection string"); cn.Open(); cm = new SqlCommand("dbo.GetAudit",cn); cm.CommandType=CommandType.StoredProcedure; //Add the parameters to be passed cm.Parameters.Add("@city",SqlDbType.Int,4); //Assign values to the parameters to be input cm.Parameters["@city"].Value=CityList.SelectedItem.Value; //Execute the stored procedure cm.ExecuteNonQuery(); SqlDataAdapter da = new SqlDataAdapter(cm); DataSet ds = new DataSet(); da.Fill(ds); DataGrid1.DataSource = ds; DataGrid1.DataBind(); Session.Add("myset2",ds); cn.Close(); lblShow.Text = "Showing Tasks for " + CityList.SelectedItem.Text + " office."; }</code> My Button click method: <code>protected void Button1_Click(object sender, System.EventArgs e) { if(CityList.SelectedIndex==0) { lblCity.Text = "Please select a location"; } else { Session.Add("city",CityList.SelectedItem.Value); BindData(); lblCity.Text = ""; } }</code> So the page loads and the GetCity() method is called. After selecting and clicking the button, the Button click event fires and the grid is displayed. Now when the checked changed even is fired, the grid disappears. Thanks for your help again, Shiv.
|
|
|
|
 |
|
 |
Hi Shiv,
what I understand here is you first load the city names in the city selector and then you fill the grid from a query with id of the city as a parameter. The grid disappears after the CheckedChanged event cause you don't get any data to rebind from your query, because you reload the city selector and don't have a selected value anymore.
Try this one for PageLoad:
private void Page_Load(object sender, System.EventArgs e)
{
if (!Page.Postback) {
GetCity();
}
}
And I'm still thinking you are better off using the BoundBooleanColumn.
Martin
|
|
|
|
 |
|
 |
why not use attributes.add("theEvent")?
Regards,
unruledboy@hotmail.com
|
|
|
|
 |
|
 |
I haven't tried that one.
First because it's not the purpose of the attributes property of the Webcontrol to register events.
Second because this seems to be the same way as to register the event as an attribute inside the control, like <asp:checkbox id="BoxInDataGrid" runat="server" oncheckedchanged="Event" />, which didn't work.
But tell me if it works.
mk
|
|
|
|
 |
|
 |
I was exploring the similar case: my DataGrid had a TemplateColumn (an imagebutton).
Added JavaScripting attributes worked fine when the DataGrid was a part of the user custom control and that user control was placed on the page without <form></form> tags.
When I placed the same user contol inside of <form></form> tags on a different page the columns of the datagrid were dynamically build correctly however scripting part was not working at all.
I had three added attributes like
'attributes.add"mousehover", "somefile.png")'
and one as 'attributes.add("onclick", "do_something")' - none of them worked
i.e. 'onclick' event was understood as a submit button click of the form
and moving the mouse over the image resulted in showing an image placeholder instead of the image itself.
I think the problem is in placing a datagrid with dynamically consructed columns inside of <form></form> tags.
I am curious if anybody had a similar problem and how it was resolved?
Thanks.
|
|
|
|
 |