Showing posts with label datagrid. Show all posts
Showing posts with label datagrid. Show all posts

Saturday, March 31, 2012

Strange behavior

Hello guys,
I have a strange behaviour in my asp.net web page. My page
contains a datagrid control, which has another nested
datagrid as a user control. It works very well, when the
user expands the + button or closes it with the same
buton. However when I do this several times successively
then suddenly I got the error message:
Object reference not set to an instance of an object.
objGetDailyStoreQuantitySold.StartDate = CType(e.Item.Cells
(0).FindControl("lblInvoiceDate"), Label).Text
When I do a refresh then it sees the label control again.
I am somehow why this is happening. Can somebody
help?
ThanksBy looking at this code, all one could really say is that:
objGetDailyStoreQuantitySold could be Nothing.
e could be Nothing.
e.Item could be Nothing
e.Item.Cells could be Nothing
...
etc.
You'll need to show a little more code or step through with a debugger
to get more details. Know what I mean?
Scott
http://www.OdeToCode.com/blogs/scott/
On Thu, 10 Feb 2005 08:01:10 -0800, "anonymous"
<anonymous@.discussions.microsoft.com> wrote:

>Hello guys,
>I have a strange behaviour in my asp.net web page. My page
>contains a datagrid control, which has another nested
>datagrid as a user control. It works very well, when the
>user expands the + button or closes it with the same
>buton. However when I do this several times successively
>then suddenly I got the error message:
>Object reference not set to an instance of an object.
>objGetDailyStoreQuantitySold.StartDate = CType(e.Item.Cells
>(0).FindControl("lblInvoiceDate"), Label).Text
>
>When I do a refresh then it sees the label control again.
>I am somehow why this is happening. Can somebody
>help?
>Thanks

Strange behavior

Hello guys,

I have a strange behaviour in my asp.net web page. My page
contains a datagrid control, which has another nested
datagrid as a user control. It works very well, when the
user expands the + button or closes it with the same
buton. However when I do this several times successively
then suddenly I got the error message:

Object reference not set to an instance of an object.
objGetDailyStoreQuantitySold.StartDate = CType(e.Item.Cells
(0).FindControl("lblInvoiceDate"), Label).Text

When I do a refresh then it sees the label control again.
I am somehow confused why this is happening. Can somebody
help?

ThanksBy looking at this code, all one could really say is that:

objGetDailyStoreQuantitySold could be Nothing.
e could be Nothing.
e.Item could be Nothing
e.Item.Cells could be Nothing
...
etc.

You'll need to show a little more code or step through with a debugger
to get more details. Know what I mean?

--
Scott
http://www.OdeToCode.com/blogs/scott/

On Thu, 10 Feb 2005 08:01:10 -0800, "anonymous"
<anonymous@.discussions.microsoft.com> wrote:

>Hello guys,
>I have a strange behaviour in my asp.net web page. My page
>contains a datagrid control, which has another nested
>datagrid as a user control. It works very well, when the
>user expands the + button or closes it with the same
>buton. However when I do this several times successively
>then suddenly I got the error message:
>Object reference not set to an instance of an object.
>objGetDailyStoreQuantitySold.StartDate = CType(e.Item.Cells
>(0).FindControl("lblInvoiceDate"), Label).Text
>
>When I do a refresh then it sees the label control again.
>I am somehow confused why this is happening. Can somebody
>help?
>Thanks

Strange behavior of invisible controls

Hello:
I'm updating multiple rows in the datagrid, using the routine described in
the article "Top Questions about the DataGrid Web Server Control":
http://msdn.microsoft.com/library/d...>
ercontrol.asp
To determine which rows had been changed, I'm using the RowChanged sub:
Protected Sub RowChanged(ByVal sender As Object, _
ByVal e As System.EventArgs)
'----
--
' Add ID's for changed rows
'----
--
Try
Dim dgi As DataGridItem = _
CType(CType(sender, Control).NamingContainer, DataGridItem)
Dim IDlabel As Label = CType(dgi.FindControl("lblOrder"), Label)
Dim ID As Integer = CType(IDlabel.Text, Integer)
If Not (IDlist.Contains(ID)) Then
IDlist.Add(ID)
End If
Catch ex As Exception
Me.lblError.Text = "Error No.: " & Err.Number.ToString & " - " & ex.ToString
End Try
End Sub
<asp:CheckBox id="chkInHouse" ToolTip="Plates In House" runat="server"
OnCheckedChanged="RowChanged" Checked='<%# DataBinder.Eval(Container,
"DataItem.InHouse") %>'>
It works OK everywhere, except one datagrid where some of my checkboxes are
invisible - the user has to click a button to make them visible. These
invisible checkboxes always indicate that their values had been changed. As
a result all my data from the datagrid is sent to the database, even if no
changes has been made. As soon as I make these checkboxes visible -
everything works OK.
Why is this happening? Is there any workaround?
I would appreciate your help.
Thank you,
Peter AfoninHi,
If you set checkbox visibility by visible property and you are using
postback to change visible state maybe there is something in the
postback activity that cause that behavior.
you can change check box visibility by changing visibility attribute of
control Style using javascrit thus reducing network traffic.
Natty Gur[MVP]
blog : http://weblogs.asp.net/ngur
Mobile: +972-(0)58-888377
*** Sent via Developersdex http://www.examnotes.net ***
Don't just participate in USENET...get rewarded for it!
Thank you, Natty.
Do you have a JavaScript sample on how to do this? I don't know JS well.
Peter
"Natty Gur" <natty@.dao2com.com> wrote in message
news:Occ$GsiPEHA.164@.TK2MSFTNGP12.phx.gbl...
> Hi,
> If you set checkbox visibility by visible property and you are using
> postback to change visible state maybe there is something in the
> postback activity that cause that behavior.
> you can change check box visibility by changing visibility attribute of
> control Style using javascrit thus reducing network traffic.
> Natty Gur[MVP]
> blog : http://weblogs.asp.net/ngur
> Mobile: +972-(0)58-888377
>
> *** Sent via Developersdex http://www.examnotes.net ***
> Don't just participate in USENET...get rewarded for it!

Strange behavior of invisible controls

Hello:

I'm updating multiple rows in the datagrid, using the routine described in
the article "Top Questions about the DataGrid Web Server Control":

http://msdn.microsoft.com/library/d...ercontrol. asp

To determine which rows had been changed, I'm using the RowChanged sub:

Protected Sub RowChanged(ByVal sender As Object, _
ByVal e As System.EventArgs)
'-----------------------
-------
' Add ID's for changed rows
'-----------------------
-------
Try
Dim dgi As DataGridItem = _
CType(CType(sender, Control).NamingContainer, DataGridItem)
Dim IDlabel As Label = CType(dgi.FindControl("lblOrder"), Label)
Dim ID As Integer = CType(IDlabel.Text, Integer)
If Not (IDlist.Contains(ID)) Then
IDlist.Add(ID)
End If
Catch ex As Exception
Me.lblError.Text = "Error No.: " & Err.Number.ToString & " - " & ex.ToString
End Try
End Sub

<asp:CheckBox id="chkInHouse" ToolTip="Plates In House" runat="server"
OnCheckedChanged="RowChanged" Checked='<%# DataBinder.Eval(Container,
"DataItem.InHouse") %>'
It works OK everywhere, except one datagrid where some of my checkboxes are
invisible - the user has to click a button to make them visible. These
invisible checkboxes always indicate that their values had been changed. As
a result all my data from the datagrid is sent to the database, even if no
changes has been made. As soon as I make these checkboxes visible -
everything works OK.

Why is this happening? Is there any workaround?

I would appreciate your help.

Thank you,

--
Peter AfoninHi,

If you set checkbox visibility by visible property and you are using
postback to change visible state maybe there is something in the
postback activity that cause that behavior.

you can change check box visibility by changing visibility attribute of
control Style using javascrit thus reducing network traffic.

Natty Gur[MVP]

blog : http://weblogs.asp.net/ngur
Mobile: +972-(0)58-888377

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Thank you, Natty.

Do you have a JavaScript sample on how to do this? I don't know JS well.

Peter

"Natty Gur" <natty@.dao2com.com> wrote in message
news:Occ$GsiPEHA.164@.TK2MSFTNGP12.phx.gbl...
> Hi,
> If you set checkbox visibility by visible property and you are using
> postback to change visible state maybe there is something in the
> postback activity that cause that behavior.
> you can change check box visibility by changing visibility attribute of
> control Style using javascrit thus reducing network traffic.
> Natty Gur[MVP]
> blog : http://weblogs.asp.net/ngur
> Mobile: +972-(0)58-888377
>
> *** Sent via Developersdex http://www.developersdex.com ***
> Don't just participate in USENET...get rewarded for it!

Monday, March 26, 2012

Strange Datagrid Update Problem

Hi,

I'm having a strange problem with the update function of a datagrid:

I have a datagrid, the first column lists item names from a database.
Second column is an EditCommandColumn, and then I have a couple of
button columns after that to deal with row deletes etc.

The problem is occuring in the method that I have specified should
deal with the update event:

protected void nameEditUpdate(object sender, DataGridCommandEventArgs
e)

Because the first column I know contains the item that is getting
updated, I try:

tmpTextBox = (TextBox)e.Item.Cells[0].Controls[0];

To get the control, before looking at its Text property. However,
when I try this I get:

System.InvalidCastException: Specified cast is not valid.

So to try and figure out what is going on, I have tried looking at the
type being returned by e.Item.Cells[0].Controls[0], which turns out to
be type System.Web.UI.LiteralControl, the name and text properties of
it are blank. This has just confused me even more.

Anyone have any idea just what might be going on?

Appreciated!Use FindControl instead of hard coding the position in the Controls collection.
Otherwise iterate over the Controls collection until you find your TextBox
(I bet it's at position Controls[1]). What's happening is that there's whitespace
in your template after the <ItemTemplate> and before your <asp:TextBox> and
that's the LiteralControl you're seeing.

-Brock
DevelopMentor
http://staff.develop.com/ballen

> Hi,
> I'm having a strange problem with the update function of a datagrid:
> I have a datagrid, the first column lists item names from a database.
> Second column is an EditCommandColumn, and then I have a couple of
> button columns after that to deal with row deletes etc.
> The problem is occuring in the method that I have specified should
> deal with the update event:
> protected void nameEditUpdate(object sender, DataGridCommandEventArgs
> e)
> Because the first column I know contains the item that is getting
> updated, I try:
> tmpTextBox = (TextBox)e.Item.Cells[0].Controls[0];
> To get the control, before looking at its Text property. However,
> when I try this I get:
> System.InvalidCastException: Specified cast is not valid.
> So to try and figure out what is going on, I have tried looking at the
> type being returned by e.Item.Cells[0].Controls[0], which turns out to
> be type System.Web.UI.LiteralControl, the name and text properties of
> it are blank. This has just confused me even more.
> Anyone have any idea just what might be going on?
> Appreciated!
Thanks Brock. I would never have guessed that whitespace would be a
member of the controls collection!

On Sat, 11 Jun 2005 07:06:34 -0700, Brock Allen
<ballen@.NOSPAMdevelop.com> wrote:

>Use FindControl instead of hard coding the position in the Controls collection.
>Otherwise iterate over the Controls collection until you find your TextBox
>(I bet it's at position Controls[1]). What's happening is that there's whitespace
>in your template after the <ItemTemplate> and before your <asp:TextBox> and
>that's the LiteralControl you're seeing.
>-Brock
>DevelopMentor
>http://staff.develop.com/ballen

Saturday, March 24, 2012

Strange error message at client

Hello!

I would like to do database change on a web page created with ASP.NET.

As soon as I click on Update in a datagrid web control, I get following
error message:

Unable to find script library
'/asp_client/system_web/1_0_3705_288/WebUIValidation.js'. Try placing this
file manually, or reinstall by running 'aspnet_regiis -c'.

I must say, that this file does exist in the path shown in the error
message. Insofar the error message is very strange.

Which are the reasons for the error? Could it be, that some ASP.NET sites
use a SmartNavigation or are the security settings at client to high?

Regards,
RobertTry running aspnet_regiis on the web site you are using. This is a
client-side error that is triggered by the file not being there, at least
from the client's perspective, so that's a good place to start.

--
Chris Jackson
Software Engineer
Microsoft MVP - Windows Shell/UI
Windows XP Associate Expert
--
More people read the newsgroups than read my email.
Reply to the newsgroup for a faster response.
(Control-G using Outlook Express)
--

"Robert Wehofer" <thalion77@.graffiti.net> wrote in message
news:c4%tb.112229$W7.62099@.news.chello.at...
> Hello!
> I would like to do database change on a web page created with ASP.NET.
> As soon as I click on Update in a datagrid web control, I get following
> error message:
> Unable to find script library
> '/asp_client/system_web/1_0_3705_288/WebUIValidation.js'. Try placing this
> file manually, or reinstall by running 'aspnet_regiis -c'.
> I must say, that this file does exist in the path shown in the error
> message. Insofar the error message is very strange.
> Which are the reasons for the error? Could it be, that some ASP.NET sites
> use a SmartNavigation or are the security settings at client to high?
> Regards,
> Robert

Strange error message

I'm getting the following error from a standard datagrid on an .aspx page, can anyone tell me what causes this?

System.Web.UI.WebControls.DataGridColumnCollection must have items of type 'System.Web.UI.WebControls.DataGridColumn'. 'table' is of type 'System.Web.UI.HtmlControls.HtmlTable'.Can you show how you have declared the DataGrid?
You are propably trying to add a HTML-Table to the Columns-collection of your DataGrid.

If you posted the code where this error occurs we will surely be able to help you out. You can usually find the line-number in the code-behind file from where the error originated. That is if you have compiled in Debug-mode.
I've not included the columns as there's a lot of code, let me know if you need them.


<ASP:DataGrid id="MyDataGrid" runat="server"
Width="500" BorderColor="black"
ShowFooter="false" CellPadding=3 CellSpacing="0"
HeaderStyle-CssClass="scroller" HeaderStyle-BackColor="#000033"
ItemStyle-CssClass="columnText" EditItemStyle-CssClass="columnText"
OnEditCommand="MyDataGrid_Edit" OnCancelCommand="MyDataGrid_Cancel"
OnUpdateCommand="MyDataGrid_Update" OnDeleteCommand="MyDataGrid_Delete"
DataKeyField="SupplierID" AutoGenerateColumns="false"
AllowPaging="true" PageSize="10" OnPageIndexChanged="MyDataGrid_Page"
PagerStyle-Mode="NumericPages" PagerStyle-HorizontalAlign="Right"
PagerStyle-Visible="true" >

Put it all, columns are crucial here.
Here is the full code:


<table width="600" border="0" align="center" cellpadding="0" cellspacing="0">
//The line above is the one causing the error
<tr>
<td>
<form id="form1" runat="server">
<table width="590" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td>
<ASP:DataGrid id="MyDataGrid" runat="server"
Width="500" BorderColor="black"
ShowFooter="false" CellPadding=3 CellSpacing="0"
HeaderStyle-CssClass="scroller" HeaderStyle-BackColor="#000033"
ItemStyle-CssClass="columnText" EditItemStyle-CssClass="columnText"
OnEditCommand="MyDataGrid_Edit" OnCancelCommand="MyDataGrid_Cancel"
OnUpdateCommand="MyDataGrid_Update" OnDeleteCommand="MyDataGrid_Delete"
DataKeyField="SupplierID" AutoGenerateColumns="false"
AllowPaging="true" PageSize="10" OnPageIndexChanged="MyDataGrid_Page"
PagerStyle-Mode="NumericPages" PagerStyle-HorizontalAlign="Right"
PagerStyle-Visible="true" >
<Columns>
<asp:EditCommandColumn HeaderText="Edit" EditText="Edit" CancelText="Cancel" UpdateText="Update" ItemStyle-Wrap="false"/>
<asp:BoundColumn HeaderText="ID" SortExpression="SupplierID" ReadOnly="True" DataField="SupplierID" ItemStyle-Wrap="false"/
<asp:TemplateColumn HeaderText="Suppliers" SortExpression="txt_Supplier">
<ItemTemplate>
<asp:Label runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "txt_Supplier") %>'/>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" id="add_txt_Supplier" Width="150" Text='<%# DataBinder.Eval(Container.DataItem, "txt_Supplier") %>' CssClass="columnText" />
</EditItemTemplate>
</asp:TemplateColumn
<asp:TemplateColumn HeaderText="Contact" SortExpression="txt_Contact">
<ItemTemplate>
<asp:Label runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "txt_Contact") %>'/>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" id="add_txt_Contact" Width="100" Text='<%# DataBinder.Eval(Container.DataItem, "txt_Contact") %>' CssClass="columnText" />
</EditItemTemplate>
</asp:TemplateColumn
<asp:TemplateColumn HeaderText="Address1" SortExpression="txt_Address1">
<ItemTemplate>
<asp:Label runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "txt_Address1") %>'/>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" id="add_txt_Address1" Width="150" Text='<%# DataBinder.Eval(Container.DataItem, "txt_Address1") %>' CssClass="columnText" />
</EditItemTemplate>
</asp:TemplateColumn
<asp:TemplateColumn HeaderText="Address2" SortExpression="txt_Address2">
<ItemTemplate>
<asp:Label runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "txt_Address2") %>'/>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" id="add_txt_Address2" Width="150" Text='<%# DataBinder.Eval(Container.DataItem, "txt_Address2") %>' CssClass="columnText" />
</EditItemTemplate>
</asp:TemplateColumn
<asp:TemplateColumn HeaderText="Town" SortExpression="txt_Town">
<ItemTemplate>
<asp:Label runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "txt_Town") %>'/>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" id="add_txt_Town" Width="100" Text='<%# DataBinder.Eval(Container.DataItem, "txt_Town") %>' CssClass="columnText" />
</EditItemTemplate>
</asp:TemplateColumn
<asp:TemplateColumn HeaderText="City" SortExpression="txt_City">
<ItemTemplate>
<asp:Label runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "txt_City") %>'/>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" id="add_txt_City" Width="100" Text='<%# DataBinder.Eval(Container.DataItem, "txt_City") %>' CssClass="columnText" />
</EditItemTemplate>
</asp:TemplateColumn
<asp:TemplateColumn HeaderText="Post Code" SortExpression="txt_PostCode">
<ItemTemplate>
<asp:Label runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "txt_PostCode") %>'/>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" id="add_txt_PostCode" Width="50" Text='<%# DataBinder.Eval(Container.DataItem, "txt_PostCode") %>' CssClass="columnText" />
</EditItemTemplate>
</asp:TemplateColumn
<asp:TemplateColumn HeaderText="Phone" SortExpression="txt_Phone">
<ItemTemplate>
<asp:Label runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "txt_Phone") %>'/>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" id="add_txt_Phone" Width="50" Text='<%# DataBinder.Eval(Container.DataItem, "txt_Phone") %>' CssClass="columnText" />
</EditItemTemplate>
</asp:TemplateColumn
<asp:TemplateColumn HeaderText="Fax" SortExpression="txt_Fax">
<ItemTemplate>
<asp:Label runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "txt_Fax") %>'/>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" id="add_txt_Fax" Width="50" Text='<%# DataBinder.Eval(Container.DataItem, "txt_Fax") %>' CssClass="columnText" />
</EditItemTemplate>
</asp:TemplateColumn
<asp:TemplateColumn HeaderText="Mobile" SortExpression="txt_Mobile">
<ItemTemplate>
<asp:Label runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "txt_Mobile") %>'/>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" id="add_txt_Mobile" Width="50" Text='<%# DataBinder.Eval(Container.DataItem, "txt_Mobile") %>' CssClass="columnText" />
</EditItemTemplate>
</asp:TemplateColumn
<asp:TemplateColumn HeaderText="Email" SortExpression="txt_Email">
<ItemTemplate>
<asp:Label runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "txt_Email") %>'/>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" id="add_txt_Email" Width="100" Text='<%# DataBinder.Eval(Container.DataItem, "txt_Email") %>' CssClass="columnText" />
</EditItemTemplate>
</asp:TemplateColumn
<asp:TemplateColumn HeaderText="Notes" SortExpression="txt_SupplierNotes">
<ItemTemplate>
<asp:Label runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "txt_SupplierNotes") %>'/>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" id="add_txt_SupplierNotes" Width="250" Text='<%# DataBinder.Eval(Container.DataItem, "txt_SupplierNotes") %>'
CssClass="columnText" TextMode="MultiLine" Rows="6"/>
</EditItemTemplate>
</asp:TemplateColumn
</ASP:DataGrid>
</td>
</tr>
</table>
</form>
</td>
</tr>
</table>

Are you sure there isn't anything before that line that could cause it? More nested grids or tables?
Nothing that I can see. This is the only datagrid on the page and the other tables on the page are completely seperate from this one.

It's pretty weird as I've created a few other similar pages without any problems.

Strange error with SqlDataReader

Hi,
I am using a datareader to fill a datagrid in a usercontrol. I use this method a lot in my application but for an unknown reason, I get this error message in only one web form: "Invalid attempt to FieldCount when reader is closed". What is strange is that the reader is actually open when the error is occuring. The only thing I see that could cause an error is that the user control is created from another usercontrol just after closing another SqlDataReader:

Calling user control


SqlDataReader rdr = cmd.ExecuteReader();
childUC = (DynamicUC)LoadControl( rdr["ASCXDynPage"].ToString() + ".ascx" );
childUC .Language = rdr["NoLanguage"].ToString();
rdr.Close();
childUC.DataBind();

Called user control (DataBind method)

... Creating SQLCommand here ...
SqlDataReader rdr = cmd.ExecuteReader();
gridNews.DataSource = rdr;
gridNews.DataBind();
gridNews.Visible = true;
rdr.Close();

After some tests, the error seam to happen when I set the DataSource of the DataGrid with the DataReader. Also, the error is set at the line where I call the DataBind method of the UserControl childUC. Anyone ever saw that kind of behavior?Maybe your query hasn´t data.
I suggest you use a dataset as datasource of the datagrid.
The query has data.
I tried the dataset, it works. But I really want to know what is wrong with that piece of code... I really don't understand what is going on...

Thursday, March 22, 2012

Strange Issue on replacing Image control with ImageButton control

Hi!

I'm using a DataGrid with a template column to display an Image inside
of it. I'm hooking into its OnPrerender-Event to set the
ImageURL-Property dynamically.
Everything works just fine here, until I thought "It would be cool, if
the user could click on that image..". So I replaced the Image-Control
with an ImageButton.

My Problem is, that the ImageButton doesn't fire any events. Any other
control works just fine but I'm having no luck getting the ImageButton
to work.

I'm happy about any ideas..

Regards
Jens

---------------
Code-Snippet

------
aspx-Page

<asp:TemplateColumn HeaderText="Images">
<ItemTemplate>
<asp:Image id="imgPic" runat="server"
OnPreRender="imgPic_PreRender"></asp:Image>
<asp:ImageButton id="ibtnPic" runat="server"
OnPreRender="ibtnPic_PreRender"></asp:ImageButton>
</ItemTemplate>
</asp:TemplateColumn
------
CodeBehind

protected void imgPic_PreRender(object sender, System.EventArgs e)
{
//this works as expected
Image img = sender as Image;
if (img == null)
{
return;
}
img.ImageURL = "dynamically-assigned.jpg";
}

protected void ibtnPic_PreRender(object sender, System.EventArgs e)
{
//this does not work
//
//if I rename this Method for testing purposes, the Page can't
//be loaded, as it doesn't find this Method
//
//if I set a Break-Point to first method, the program does not
//stop
ImageButton ibtn = sender as ImageButton;
if (ibtn == null)
{
return;
}
ibtn.ImageURL = "dynamically-assigned.jpg";
}Jens,

With thwe ImageButton you can setup both the Click and Command events. Do
you setup one of them?

Eliyahu

<jens.buchta@.gmx.net> wrote in message
news:1125557636.729770.63420@.g49g2000cwa.googlegro ups.com...
> Hi!
> I'm using a DataGrid with a template column to display an Image inside
> of it. I'm hooking into its OnPrerender-Event to set the
> ImageURL-Property dynamically.
> Everything works just fine here, until I thought "It would be cool, if
> the user could click on that image..". So I replaced the Image-Control
> with an ImageButton.
> My Problem is, that the ImageButton doesn't fire any events. Any other
> control works just fine but I'm having no luck getting the ImageButton
> to work.
> I'm happy about any ideas..
> Regards
> Jens
>
> ---------------
> Code-Snippet
> ------
> aspx-Page
> <asp:TemplateColumn HeaderText="Images">
> <ItemTemplate>
> <asp:Image id="imgPic" runat="server"
> OnPreRender="imgPic_PreRender"></asp:Image>
> <asp:ImageButton id="ibtnPic" runat="server"
> OnPreRender="ibtnPic_PreRender"></asp:ImageButton>
> </ItemTemplate>
> </asp:TemplateColumn>
> ------
> CodeBehind
> protected void imgPic_PreRender(object sender, System.EventArgs e)
> {
> //this works as expected
> Image img = sender as Image;
> if (img == null)
> {
> return;
> }
> img.ImageURL = "dynamically-assigned.jpg";
> }
> protected void ibtnPic_PreRender(object sender, System.EventArgs e)
> {
> //this does not work
> //
> //if I rename this Method for testing purposes, the Page can't
> //be loaded, as it doesn't find this Method
> //
> //if I set a Break-Point to first method, the program does not
> //stop
> ImageButton ibtn = sender as ImageButton;
> if (ibtn == null)
> {
> return;
> }
> ibtn.ImageURL = "dynamically-assigned.jpg";
> }
Thanks for your Answer!

I can set up the Click event to parse the CommandName and
CommandArgument. This is a way to react on the users PostBack, no
problem here.

The problem is, that i need the PreRender event to handle the ImageURL
before the ImageButton is drawn. At the moment, I can't catch this
event in the same way, as it works for the Image-control, so that the
image, the ImageButtons renders, is "none".

Jens
Why can't you handle the ImageUrl in Click event?

Eliyahu

<jens.buchta@.gmx.net> wrote in message
news:1125566088.815538.95660@.o13g2000cwo.googlegro ups.com...
> Thanks for your Answer!
> I can set up the Click event to parse the CommandName and
> CommandArgument. This is a way to react on the users PostBack, no
> problem here.
> The problem is, that i need the PreRender event to handle the ImageURL
> before the ImageButton is drawn. At the moment, I can't catch this
> event in the same way, as it works for the Image-control, so that the
> image, the ImageButtons renders, is "none".
> Jens

Strange Issue on replacing Image control with ImageButton control

Hi!
I'm using a DataGrid with a template column to display an Image inside
of it. I'm hooking into its OnPrerender-Event to set the
ImageURL-Property dynamically.
Everything works just fine here, until I thought "It would be , if
the user could click on that image..". So I replaced the Image-Control
with an ImageButton.
My Problem is, that the ImageButton doesn't fire any events. Any other
control works just fine but I'm having no luck getting the ImageButton
to work.
I'm happy about any ideas..
Regards
Jens
---
Code-Snippet
aspx-Page
<asp:TemplateColumn HeaderText="Images">
<ItemTemplate>
<asp:Image id="imgPic" runat="server"
OnPreRender="imgPic_PreRender"></asp:Image>
<asp:ImageButton id="ibtnPic" runat="server"
OnPreRender="ibtnPic_PreRender"></asp:ImageButton>
</ItemTemplate>
</asp:TemplateColumn>
CodeBehind
protected void imgPic_PreRender(object sender, System.EventArgs e)
{
//this works as expected
Image img = sender as Image;
if (img == null)
{
return;
}
img.ImageURL = "dynamically-assigned.jpg";
}
protected void ibtnPic_PreRender(object sender, System.EventArgs e)
{
//this does not work
//
//if I rename this Method for testing purposes, the Page can't
//be loaded, as it doesn't find this Method
//
//if I set a Break-Point to first method, the program does not
//stop
ImageButton ibtn = sender as ImageButton;
if (ibtn == null)
{
return;
}
ibtn.ImageURL = "dynamically-assigned.jpg";
}Jens,
With thwe ImageButton you can setup both the Click and Command events. Do
you setup one of them?
Eliyahu
<jens.buchta@.gmx.net> wrote in message
news:1125557636.729770.63420@.g49g2000cwa.googlegroups.com...
> Hi!
> I'm using a DataGrid with a template column to display an Image inside
> of it. I'm hooking into its OnPrerender-Event to set the
> ImageURL-Property dynamically.
> Everything works just fine here, until I thought "It would be , if
> the user could click on that image..". So I replaced the Image-Control
> with an ImageButton.
> My Problem is, that the ImageButton doesn't fire any events. Any other
> control works just fine but I'm having no luck getting the ImageButton
> to work.
> I'm happy about any ideas..
> Regards
> Jens
>
> ---
> Code-Snippet
> --
> aspx-Page
> <asp:TemplateColumn HeaderText="Images">
> <ItemTemplate>
> <asp:Image id="imgPic" runat="server"
> OnPreRender="imgPic_PreRender"></asp:Image>
> <asp:ImageButton id="ibtnPic" runat="server"
> OnPreRender="ibtnPic_PreRender"></asp:ImageButton>
> </ItemTemplate>
> </asp:TemplateColumn>
> --
> CodeBehind
> protected void imgPic_PreRender(object sender, System.EventArgs e)
> {
> //this works as expected
> Image img = sender as Image;
> if (img == null)
> {
> return;
> }
> img.ImageURL = "dynamically-assigned.jpg";
> }
> protected void ibtnPic_PreRender(object sender, System.EventArgs e)
> {
> //this does not work
> //
> //if I rename this Method for testing purposes, the Page can't
> //be loaded, as it doesn't find this Method
> //
> //if I set a Break-Point to first method, the program does not
> //stop
> ImageButton ibtn = sender as ImageButton;
> if (ibtn == null)
> {
> return;
> }
> ibtn.ImageURL = "dynamically-assigned.jpg";
> }
>
Thanks for your Answer!
I can set up the Click event to parse the CommandName and
CommandArgument. This is a way to react on the users PostBack, no
problem here.
The problem is, that i need the PreRender event to handle the ImageURL
before the ImageButton is drawn. At the moment, I can't catch this
event in the same way, as it works for the Image-control, so that the
image, the ImageButtons renders, is "none".
Jens
Why can't you handle the ImageUrl in Click event?
Eliyahu
<jens.buchta@.gmx.net> wrote in message
news:1125566088.815538.95660@.o13g2000cwo.googlegroups.com...
> Thanks for your Answer!
> I can set up the Click event to parse the CommandName and
> CommandArgument. This is a way to react on the users PostBack, no
> problem here.
> The problem is, that i need the PreRender event to handle the ImageURL
> before the ImageButton is drawn. At the moment, I can't catch this
> event in the same way, as it works for the Image-control, so that the
> image, the ImageButtons renders, is "none".
> Jens
>

Tuesday, March 13, 2012

strange postback/ SQL problem

I have a datagrid which populates from a SQLDataSource. The page is to default to filter the data as the user that is logged on but there is a drop down menu to select to filter by another user. The drop down menu is autopostback and I have the following code in my on page load event:

 Dim teamleader As String Dim currentyear As String = DateTime.Now.Year 'find the current year
 If Page.IsPostBack Then 'pickup the selected teamleader from drop down menu Dim placeholder As ContentPlaceHolder = CType(Master.FindControl("bodybox"), ContentPlaceHolder) Dim thelist As DropDownList = CType(placeholder.FindControl("dropdownlist1"), DropDownList) teamleader = thelist.Text.ToString 'default to logged in persons name Else teamleader = User.Identity.Name.ToString End If teamdata.SelectCommand = "SELECT username, name, holiday_entitlement_" & currentyear & " as holiday_entitlement, holiday_used_" & currentyear & " as holiday_used, holiday_entitlement_" & currentyear & " - holiday_used_" & currentyear & " AS holiday_left FROM holiday.reps WHERE (teamleader = '" & teamleader & "') AND (hasleft = 'N') ORDER BY name;"

The problem is that if the page isnt postback and the user logged in is "ch12" then I get a populated datagrid. If however the page is a postback the datagrid is showing the empty template. I have looked at the SQL the page is generating and as far as I see they are identical:

Not postback:

SELECT username,name, holiday_entitlement_2006as holiday_entitlement, holiday_used_2006as holiday_used, holiday_entitlement_2006 - holiday_used_2006AS holiday_leftFROM holiday.repsWHERE (teamleader ='ch12')AND (hasleft ='N')ORDER BY name;

Is postback:

SELECT username,name, holiday_entitlement_2006as holiday_entitlement, holiday_used_2006as holiday_used, holiday_entitlement_2006 - holiday_used_2006AS holiday_leftFROM holiday.repsWHERE (teamleader ='ch12')AND (hasleft ='N')ORDER BY name;

Any ideas why one datagrid is appearing empty and the other populated when the select command being generated appears to be identical?

Anyone have any idea on this at all?

Set the datasource to the Grid in the SelectedIndexChanged event of the dropdownlist. Make sure you populate the dropdownlist with PostBack check in the page_load.

page_load() {

if (!Page.IsPostBack) {
// load the dropdownlist
// assign datasource and bind grid -- default
}
}

selectedIndexChanged event of dropdownlist {
// assign datasource and bind grid
}

Thanks


Have worked out the problem but not the fix. I am correctly creating the new sqldatasource1.selectcommand on the postback however the datagrid is always displaying the results from the original page load.

How do I make the page discard the original results and re run the query on each page load (in VB)?

Many thanks



1<%@. Page Language="VB" MasterPageFile="~/loggedin/the.master" %>23<script runat="server">45Protected Sub Page_Load(ByVal senderAs Object,ByVal eAs System.EventArgs6Dim teamleader as string7Dim currentyearAs String = DateTime.Now.Year8If Not User.IsInRole("teamleader")Then9 Try10 Dim placeholderAs ContentPlaceHolder =CType(Master.FindControl("bodybox"), ContentPlaceHolder)11Dim thelistAs DropDownList =CType(placeholder.FindControl("dropdownlist1"), DropDownList)12 teamleader = thelist.Text.ToString'default to logged in persons name13If teamleader ="."Then teamleader = User.Identity.Name.ToString14Catch exAs Exception15 teamleader = User.Identity.Name.ToString16End Try17 Else18 teamleader = User.Identity.Name.ToString19End If2021 teamdata.SelectCommand ="SELECT username, name, holiday_entitlement_" & currentyear &" as holiday_entitlement, holiday_used_" & currentyear &" as holiday_used, holiday_entitlement_" & currentyear &" - holiday_used_" & currentyear &" AS holiday_left FROM holiday.reps WHERE (teamleader ='" & teamleader & "') AND (hasleft = 'N') ORDER BY name;"22 End Sub23</script>2425<asp:Content ID="Content1" ContentPlaceHolderID="bodybox" Runat="Server">2627 <asp:DropDownList ID="dropdownlist1" runat="server" DataSourceID="teamleaderlist" DataTextField="Username" DataValueField="Username" AutoPostBack="true" AppendDataBoundItems="True" >28 <asp:ListItem Selected="True" Value="." Text=""></asp:ListItem></asp:DropDownList>29 <asp:SqlDataSource ID="teamleaderlist" runat="server" ConnectionString="<%$ ConnectionStrings:holidaybookingConnectionString %>" ProviderName="<%$ ConnectionStrings:holidaybookingConnectionString.ProviderName %>"></asp:SqlDataSource>3031<asp:SqlDataSource ID="teamdata" runat="server" ConnectionString="<%$ ConnectionStrings:holidaybookingConnectionString %>" ProviderName="<%$ ConnectionStrings:holidaybookingConnectionString.ProviderName %>" ></asp:SqlDataSource>3233<asp:GridView ID="teamview" runat="server" AllowSorting="True"34 DataKeyNames="username" DataSourceID="teamdata" CellPadding="5" GridLines="None" HorizontalAlign="Center">35 <EmptyDataTemplate>36 There are currently no Reps to display37 </EmptyDataTemplate>38 </asp:GridView>39</asp:content>
Above is the code for the problem area - I havent posted the code generating the SQL for the dropdownmenu as it is rather long and convoluted and is working fine.

Post your code!

Thanks


DropDownList onselectedIndexchanged will cause postback and your page_load code executes everytime it is posted. So you are getting the same values. Add a postback check in the page_load. Where in the page_load you are binding the grid?? In the SelectIndexChanged event again you need to write the code for the selected userRole


5Protected Sub Page_Load(ByVal senderAs Object,ByVal eAs System.EventArgs
6Dim teamleader as string
7Dim currentyearAs String = DateTime.Now.Year
If Not Page.IsPostBack Then
8If Not User.IsInRole("teamleader")Then
9 Try
10 Dim placeholderAs ContentPlaceHolder =CType(Master.FindControl("bodybox"), ContentPlaceHolder)
11Dim thelistAs DropDownList =CType(placeholder.FindControl("dropdownlist1"), DropDownList)
12 teamleader = thelist.Text.ToString'default to logged in persons name
13If teamleader ="."Then teamleader = User.Identity.Name.ToString
14Catch exAs Exception
15 teamleader = User.Identity.Name.ToString
16End Try
17 Else
18 teamleader = User.Identity.Name.ToString
19End If
20
21 teamdata.SelectCommand ="SELECT username, name, holiday_entitlement_" & currentyear &" as holiday_entitlement, holiday_used_" & currentyear &" as holiday_used, holiday_entitlement_" & currentyear &" - holiday_used_" & currentyear &" AS holiday_left FROM holiday.reps WHERE (teamleader ='" & teamleader & "') AND (hasleft = 'N') ORDER BY name;"
End If
protected void DropdownList1_SelectedIndexChanged(object sender, EventArgs e)
{

6Dim teamleader as string
7Dim currentyearAs String = DateTime.Now.Year
8If Not User.IsInRole("teamleader")Then
9 Try
10 Dim placeholderAs ContentPlaceHolder =CType(Master.FindControl("bodybox"), ContentPlaceHolder)
11Dim thelistAs DropDownList =CType(placeholder.FindControl("dropdownlist1"), DropDownList)
12 teamleader = thelist.Text.ToString'default to logged in persons name
13If teamleader ="."Then teamleader = User.Identity.Name.ToString
14Catch exAs Exception
15 teamleader = User.Identity.Name.ToString
16End Try
17 Else
18 teamleader = User.Identity.Name.ToString
19End If
20
21 teamdata.SelectCommand ="SELECT username, name, holiday_entitlement_" & currentyear &" as holiday_entitlement, holiday_used_" & currentyear &" as holiday_used, holiday_entitlement_" & currentyear &" - holiday_used_" & currentyear &" AS holiday_left FROM holiday.reps WHERE (teamleader ='" & teamleader & "') AND (hasleft = 'N') ORDER BY name;"

}
Also try using the dropDownList.SelectedItem.Value rather the .Text when selecting values fromthe dropdownlist.

Thanks
Many thanks, all works great now