Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 3.5 > ASP.NET 3.5 Professionals
|
ASP.NET 3.5 Professionals If you are an experienced ASP.NET programmer, this is the forum for your 3.5 questions. Please also see the Visual Web Developer 2008 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 3.5 Professionals section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 3rd, 2009, 07:55 AM
Authorized User
 
Join Date: Apr 2007
Posts: 61
Thanks: 0
Thanked 0 Times in 0 Posts
Default GridView inside UpdatePanel not Uploading Image

Hello Friends,

I've a GridView inside a UpdatePanel. And there is a Fileupload control inside the GridView which is fired to Update the existing image with newer one. My code is running perfectly without UpdatePanel. But as i put my GridView inside the UpdatePanel it gives me an error "Object References not set to an instance". I think this error is being generated because of FileUpload control. So i want to have a trigger control of UpdatePanel to upload the new image. How can i bind a trigger control of UpdatePanel at RowUpdating event of GridView at code behind
 
Old September 10th, 2009, 09:19 PM
Authorized User
 
Join Date: Jun 2009
Posts: 18
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Hi,
I just implemented your function in my application fews day ago.I also used a fileupload control in a cell of tht row,and add a upload button besides it.
I have my code pasted below:
aspx code:
Code:
                    <EditItemTemplate>
                        <asp:FileUpload ID="fuUpload" runat="server" />
                        <asp:Button ID="btnUpdateImage" runat="server" CommandArgument='<%#Eval("CategoryID") %>'
                            CommandName="upload" Text="上传" />
                    </EditItemTemplate>
                    <ItemTemplate>
                        <!--用ajax实现gridview的图片特效-->
                        <img id="image" alt="" runat="server" src='<%# "GetCategoryImage.aspx?ID=" + Eval("CategoryID") %>'
                            height="80" width="70" />
                        <!--<asp:Image ID="Image1" runat="server" Height="90" ImageUrl='<%# "GetCategoryImage.aspx?ID=" + Eval("CategoryID") %>'
                            Width="130" />-->
                    </ItemTemplate>
                    <ItemStyle Height="160px" />
                </asp:TemplateField>

and aspx.cs code below:
Code:
 protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "upload")
        {
            GridViewRow gvr = ((GridViewRow)(((Button)(e.CommandSource)).Parent.Parent));//此得出的值是表示那行被选中的索引值
            int Key = Convert.ToInt32(GridView1.DataKeys[gvr.RowIndex].Value);
            FileUpload fu = (FileUpload)this.GridView1.Rows[gvr.RowIndex].Cells[1].FindControl("fuUpload");
            int updateImagelength = fu.PostedFile.ContentLength;
            byte[] updateimage = new byte[updateImagelength];
            fu.PostedFile.InputStream.Read(updateimage, 0, updateImagelength);
            using (NorthwindEntities ctx = new NorthwindEntities())
            {
                var category = ctx.Categories.First(p => p.CategoryID == Key);
                category.Picture = updateimage;
                ctx.SaveChanges();
            }
        }
    }
And with this,you can update your image to DB,I think it could help.If you've got anything with this,tell me,thanks!
 
Old September 15th, 2009, 03:16 AM
Authorized User
 
Join Date: Mar 2008
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to myself.panku
Default

Hi Manoj,

I am not sure that i am being answered what u have asked for, but i could not see the update panel there in code to make some correction, you might have not thought that code important to paste here too.

So I am just writing down a sample code which is used to bind an event with the trigger in update panel.

Example :

<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:GridView ID="GridView1" runat="server" onrowdatabound="GridView1_RowDataBound">
</asp:GridView>
</ContentTemplate>

<Triggers>
<asp:AsyncPostBackTrigger ControlID="GridView1" EventName="rowdatabound" />
</Triggers>

</asp:UpdatePanel>

That is how it is happened in general term

I hope it will help you...

Thanks
-Pankaj Bahuguna
__________________
Thanks
-Pankaj Bahuguna
 
Old September 15th, 2009, 06:13 AM
Authorized User
 
Join Date: Apr 2007
Posts: 61
Thanks: 0
Thanked 0 Times in 0 Posts
Default

<asp:UpdatePanelID="updatePanel1"UpdateMode="Conditional"runat="server">
<ContentTemplate>
<asp:LabelID="lblMsg1"runat="server"></asp:Label><br/>
<asp:GridViewID="GridView1"runat="server"DataKeyNames="ModelID"AutoGenerateColumns="False"onrowcancelingedit="GridView1_RowCancelingEdit"EmptyDataText="No Record(s) Found!!"EmptyDataRowStyle-HorizontalAlign="Center"
EmptyDataRowStyle-Font-Bold="true"
AlternatingRowStyle-CssClass="GridView_Row_Alternate"Width="100%"GridLines="None"CssClass="GridView_Body"AllowPaging="True"onrowediting="GridView1_RowEditing"onrowdatabound="GridView1_RowDataBound"onrowdeleting="GridView1_RowDeleting"onrowupdating="GridView1_RowUpdating"onpageindexchanging="GridView1_PageIndexChanging"PageSize="50">
<Columns><asp:TemplateField>
<HeaderTemplate></HeaderTemplate>
<HeaderStyleCssClass="GridView_Header"/><ItemTemplate></ItemTemplate></asp:TemplateField><asp:TemplateField>
<HeaderTemplate>Model ID</HeaderTemplate>
<HeaderStyleCssClass="GridView_Header"/>
<ItemTemplate>
<asp:LabelID="lblModelID"runat="server"Text='<%#Eval("ModelID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField><asp:TemplateField>
<HeaderTemplate>Make ID</HeaderTemplate>
<HeaderStyleCssClass="GridView_Header"/>
<ItemTemplate>
<asp:LabelID="lblMakeID"runat="server"Text='<%#Eval("MakeID") %>'></asp:Label></ItemTemplate>
</asp:TemplateField><asp:TemplateField>
<HeaderTemplate>Make Year</HeaderTemplate>
<HeaderStyleCssClass="GridView_Header"/>
<ItemTemplate>
<asp:LabelID="lblMakeYear"runat="server"Text='<%#Eval("MakeYear") %>'></asp:Label></ItemTemplate></asp:TemplateField><asp:TemplateField>
<HeaderTemplate>Make Name</HeaderTemplate>
<HeaderStyleCssClass="GridView_Header"/>
<ItemTemplate>
<asp:LabelID="lblMakeName"runat="server"ToolTip='<%#Eval("MakeNameID") %>'Text='<%#Eval("MakeName") %>'></asp:Label>
</ItemTemplate></asp:TemplateField>
<asp:TemplateField><HeaderTemplate>Model Name</HeaderTemplate>
<HeaderStyleCssClass="GridView_Header"/>
<ItemTemplate><asp:LabelID="lblModelName"runat="server"Text='<%#Eval("ModelName") %>'></asp:Label></ItemTemplate><EditItemTemplate><asp:DropDownListID="ddlModelName_Edit"SkinID="Dropdown1"runat="server"></asp:DropDownList><asp:UpdateProgressID="updateProgress3_GridView"runat="server">
<ProgressTemplate>
<imgsrc="../../Images/General/Updating.gif"height="10"width="10"/>
<spanstyle="font-style:italic;font-family:Arial;font-size:8.5pt;">Please wait....</span></ProgressTemplate>
</asp:UpdateProgress>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField><HeaderTemplate>Small Image</HeaderTemplate>
<HeaderStyleCssClass="GridView_Header"/>
<ItemTemplate><asp:ImageID="image_Small"Height="50"Width="50"ImageUrl='<%#getFullPath(Eval("Model_SmallImage")) %>'runat="server"/></ItemTemplate><EditItemTemplate>
<asp:FileUploadID="image_Small_Edit"ToolTip='<%#Eval("Model_SmallImage") %>'SkinID="Upload1"runat="server"/>
</EditItemTemplate></asp:TemplateField>
<asp:CommandFieldHeaderText="Edit"ControlStyle-CssClass="A4"HeaderStyle-CssClass="GridView_Header"ShowEditButton="true"><ControlStyleCssClass="A4"/>
<HeaderStyleCssClass="GridView_Header"/>
</asp:CommandField>
<asp:TemplateField>
<HeaderTemplate>Delete</HeaderTemplate>
<HeaderStyleCssClass="GridView_Header"/>
<ItemTemplate>
<asp:LinkButtonID="lnkDelete"runat="server"CssClass="A4"OnClientClick="return confirm('Want to delete this record');"CommandName="Delete"Text="Delete"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField></Columns><RowStyleCssClass="GridView_Row"/>
<AlternatingRowStyleCssClass="GridView_Row_Alternate"/>
<EditRowStyleHeight="50"/>
</asp:GridView>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTriggerControlID="GridView1"EventName="rowdatabound"/>
</Triggers>
</asp:UpdatePanel>


At Code-Behind

protectedvoid GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
FileUpload image_Small_Edit = (FileUpload)gr.Cells[5].FindControl("image_Small_Edit");
string smallImage = null;

//image_Small_Edit.PostedFile.FileName is null....
smallImage = image_Small_Edit.PostedFile.FileName;
......................................
}
and that's why it's throwing "Object reference not set to an instance"

Quote:
Originally Posted by myself.panku View Post
Hi Manoj,

I am not sure that i am being answered what u have asked for, but i could not see the update panel there in code to make some correction, you might have not thought that code important to paste here too.

So I am just writing down a sample code which is used to bind an event with the trigger in update panel.

Example :

<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:GridView ID="GridView1" runat="server" onrowdatabound="GridView1_RowDataBound">
</asp:GridView>
</ContentTemplate>

<Triggers>
<asp:AsyncPostBackTrigger ControlID="GridView1" EventName="rowdatabound" />
</Triggers>

</asp:UpdatePanel>

That is how it is happened in general term

I hope it will help you...

Thanks
-Pankaj Bahuguna
 
Old September 15th, 2009, 07:29 AM
Authorized User
 
Join Date: Mar 2008
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to myself.panku
Default

Hi Manoj,

I have seen your code and I felt this code is okay for rowEditing event of grid but not for rowdatabound.

protectedvoid GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
FileUpload image_Small_Edit = (FileUpload)gr.Cells[5].FindControl("image_Small_Edit");
string smallImage = null;

//image_Small_Edit.PostedFile.FileName is null....
smallImage = image_Small_Edit.PostedFile.FileName;
......................................
}

the code i have highlighted overhere will be running all the time when grid is bound without taking care whether grid is in edit mode or not.

So as per the code i can see control id image_Small_Editbelongs to edit item template state only.

So u need to make a patch inside rowdata bound like :

// this peice of code will be executing only in case when u r in edititemindex state.

protectedvoid GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if ( gr.EditItemIndex != -1 )
{
FileUpload image_Small_Edit = (FileUpload)gr.Cells[5].FindControl("image_Small_Edit");

instead of using this complex syntax with INDEX(static) as u have wriiten just above, i will suggest u this one it will not take care of index , it is dynamic and will not be affected if in future u placed ur desired column in any other position
FileUpload image_Small_Edit = (FileUpload)gr.FindControl("image_Small_Edit");

string smallImage = null;

//image_Small_Edit.PostedFile.FileName is null....
smallImage = image_Small_Edit.PostedFile.FileName;
}
else //if required
{
//whatever u want
}
} // end of event

__________________
Thanks
-Pankaj Bahuguna
 
Old September 15th, 2009, 07:40 AM
Authorized User
 
Join Date: Mar 2008
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to myself.panku
Default

There was a mistake in code...please replace that code with this one,


if ( gr.EditItemIndex != -1 )

gr : is a GridViewRow object whereas I need to use GridView object over here

So change it by

if ( YourGridViewID.EditItemIndex != -1 )

I hope it should help you......

__________________
Thanks
-Pankaj Bahuguna
 
Old September 15th, 2009, 09:03 AM
Authorized User
 
Join Date: Apr 2007
Posts: 61
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Well EditItemIndex is not coming my GridView id is GridView1
i'm giving my RowDataBound code again please let me know what changes to be made

GridViewRow gr = e.Row;
if ((gr.RowType == DataControlRowType.DataRow) && (gr.RowState & DataControlRowState.Edit) > 0)
{
if (GridView1.EditIndex != -1)
{
//FileUpload image_Small_Edit = (FileUpload)gr.Cells[5].FindControl("image_Small_Edit");
FileUpload image_Small_Edit = (FileUpload)gr.FindControl("image_Small_Edit");
string smallImage = null;
smallImage = image_Small_Edit.PostedFile.FileName;
}
}

Quote:
Originally Posted by myself.panku View Post
There was a mistake in code...please replace that code with this one,


if ( gr.EditItemIndex != -1 )

gr : is a GridViewRow object whereas I need to use GridView object over here

So change it by

if ( YourGridViewID.EditItemIndex != -1 )

I hope it should help you......

 
Old September 15th, 2009, 10:48 AM
Authorized User
 
Join Date: Mar 2008
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to myself.panku
Default

GridViewRow gr = e.Row;

if
(GridView1.EditIndex != -1 && e.Row.RowIndex != -1 )
{
//FileUpload image_Small_Edit = (FileUpload)gr.Cells[5].FindControl("image_Small_Edit");
FileUpload image_Small_Edit = (FileUpload)gr.FindControl("image_Small_Edit");
string smallImage = null;
smallImage = image_Small_Edit.PostedFile.FileName;
}


It was my bad .EditIndex is the right property.
But I think U should go with a single patch.

Try with this one.
it might help u...
__________________
Thanks
-Pankaj Bahuguna
 
Old September 16th, 2009, 03:34 AM
Authorized User
 
Join Date: Apr 2007
Posts: 61
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Sorry bro, still getting same error "Object reference not set to an instance"

Quote:
Originally Posted by myself.panku View Post
GridViewRow gr = e.Row;

if
(GridView1.EditIndex != -1 && e.Row.RowIndex != -1 )
{
//FileUpload image_Small_Edit = (FileUpload)gr.Cells[5].FindControl("image_Small_Edit");
FileUpload image_Small_Edit = (FileUpload)gr.FindControl("image_Small_Edit");
string smallImage = null;
smallImage = image_Small_Edit.PostedFile.FileName;
}


It was my bad .EditIndex is the right property.
But I think U should go with a single patch.

Try with this one.
it might help u...





Similar Threads
Thread Thread Starter Forum Replies Last Post
How to tie to nested GridView inside DataList rsearing ASP.NET 2.0 Basics 6 June 18th, 2011 06:35 AM
Two DropDownList Inside a Gridview ilayaraja ASP.NET 3.5 Basics 3 December 5th, 2008 06:26 PM
Default image generation while uploading an image! ostwald ASP.NET 2.0 Professional 1 September 12th, 2007 01:44 AM
TextBox inside GridView michurin ASP.NET 2.0 Basics 2 January 13th, 2007 12:51 PM
DropDownList inside a GridView aidoco ASP.NET 2.0 Basics 0 October 13th, 2006 04:24 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.