且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在gridview中预览图片而不使用Handler

更新时间:2023-12-04 10:52:34

<form id="form1" runat="server">
   <div>

       <img id="ImagePreview"  src="" />
       <input type="file" id="files" name="files[]" multiple />

       <script type="text/javascript">


           function handleFileSelect(evt) {
               var fileUpload = document.getElementById('files');
               var files = evt.target.files; // FileList object

               for (var i = 0, f; f = files[i]; i++) {
                   var reader = new FileReader();
                   var base64Data;
                   reader.onload = (function (theFile) {
                       return function (e) {
                           base64Data = e.target.result;
                           document.getElementById('ImagePreview').src = base64Data;
                       };
                   })(f);
                   // Read in the image file as a data URL.
                   reader.readAsDataURL(f);
               }
           }

           document.getElementById('files').addEventListener('change', handleFileSelect, false);
       </script>

       <div>
       </div>
   </div>
   </form>







这是示例代码在没有任何回发的情况下在图像控件中预览图像。




This is the sample code to preview the image in image control without any postback.


好的,根据你的解释,你想在不同的页面中使用图像,是吗?

First你所有的错误都在:



Ok, According to your explanation, you want to use the image in different page, is it?
First of all your error is in:

<asp:image runat="server" id="student_Pic" imageurl="~/frmImagePrev.aspx" imagealign="Middle" alternatetext="Insert Picture" xmlns:asp="#unknown"></asp:image>





它应该是:





it should be:

<a href="WebForm2.aspx">
    <asp:Image runat="server" ID="student_Pic" ImageUrl="~/images/image_link.jpg"  ImageAlign="Middle" AlternateText="Insert Picture"/>
    </a>





因为你正在使用图片作为图片按钮好吗?

接下来,你想要保持你的图像价值在会话上(根据你的解释)吧?

我建议你不要在会话中存储图像字节,因为会话中的图像字节会使你的页面变慢。你可以找到另一种方式。



但如果你真的想在会话中存储图像byte []然后将图像转换为byte []然后存储byte []值在会议中,不仅仅是图片名称。



我再次要求你说明你的解释不是100%明确的。



谢谢。



Rashed ::孟加拉国。



because you are using the image as a image button ok?
next, you want to keep your image value on session( according to your explanation) right?
I will suggest you no to store image byte in session because, image byte in session will make your page to slow. you can find alternative way.

but if you really want to store image byte[] in session then convert the image to byte[] then store the byte[] value in session, not only image name.

again I am requesting you that your explanation is not 100% clear to me.

Thank you.

Rashed::Bangladesh.


我想我的解释不清楚你

让我们举个例子

要求:

1.有一个gridview

2.它必须有编辑每行中的/删除按钮

I think my explanation is not clear to u
Lets take a example
Requirement :
1. There is one gridview
2. It must have edit/delete button in every row
AutoGenerateEditButton="True"

在gridview中使用它。

3.用户点击时在编辑按钮中,整行(用户单击的位置)显示更新和取消按钮以及以可编辑模式打开的网格。根据我的网格,它有两个fileupload选项

我的网格是

using this in gridview.
3. When user click in the edit button the entire row (Where user click) shows the update and cancel button and grid opened in editable mode. According to my grid it have two fileupload option
My grid is

<asp:GridView ID="gdvstudent" runat="server" AutoGenerateColumns="False" AutoGenerateEditButton="True"

            CellPadding="4" AllowSorting="True" OnSorting="gdvstudent_Sorting" ForeColor="#333333"

            ShowFooter="true" SortedAscendingCellStyle-BackColor="Blue" HorizontalAlign="Center"

            AllowPaging="True" PageSize="5" Width="754px">
            <AlternatingRowStyle BackColor="White" />
            <Columns>
                <asp:TemplateField HeaderText="Student Name" SortExpression="txt_student_name">
                    <ItemTemplate>
                        <%#Eval("txt_student_name")%></ItemTemplate>
                    <EditItemTemplate>
                        <asp:TextBox ID="txtStudentName" runat="server" Text='<%#Eval("txt_student_name")%>'></asp:TextBox>
                    </EditItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Student Roll" SortExpression="txt_student_roll">
                    <ItemTemplate>
                        <%#Eval("txt_student_roll")%></ItemTemplate>
                    <EditItemTemplate>
                        <asp:Label ID="txtstudentroll" runat="server" Text='<%#Eval("txt_student_roll")%>'></asp:Label>
                    </EditItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Student class" SortExpression="txt_student_class">
                    <ItemTemplate>
                        <%#Eval("txt_student_class")%></ItemTemplate>
                    <EditItemTemplate>
                        <asp:TextBox ID="txtstudentclass" runat="server" Text='<%#Eval("txt_student_class")%>'></asp:TextBox>
                    </EditItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Student Marks" SortExpression="txt_student_marks">
                    <ItemTemplate>
                        <%#Eval("txt_student_marks")%></ItemTemplate>
                    <EditItemTemplate>
                        <asp:TextBox ID="txtstudentmarks" runat="server" Text='<%#Eval("txt_student_marks")%>'></asp:TextBox>
                    </EditItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Student DOB" SortExpression="txt_student_dob">
                    <ItemTemplate>
                        <%#Eval("txt_student_dob")%></ItemTemplate>
                    <EditItemTemplate>
                        <asp:TextBox ID="txtstudentdob" runat="server" Text='<%#Eval("txt_student_dob")%>'></asp:TextBox>
                    </EditItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Student Resume">
                    <ItemTemplate>
                        <asp:LinkButton runat="server" ID="student_resume" CommandArgument='<%# Bind("student_resume") %>'

                            Visible="false" OnClick="DownloadFile" Text="Resume">
                        </asp:LinkButton>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:FileUpload ID="fileupload" FileName='<%# Bind("student_resume") %>' runat="server">
                        </asp:FileUpload>
                    </EditItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Student Picture">
                    <ItemTemplate>
                        <asp:Image runat="server" ID="student_Pic" ImageUrl="~/frmImagePrev.aspx" ImageAlign="Middle" AlternateText="Insert Picture"></asp:Image>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:FileUpload ID="fileupload1" FileName='<%# Bind("student_pic") %>' runat="server">
                        </asp:FileUpload>
                    </EditItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Edited" Visible="False">
                    <ItemTemplate>
                        <asp:CheckBox ID="ChkboxEdit" runat="server" Checked='<%#Eval("edited")%>' />
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:CheckBox ID="ChkboxEdit" runat="server" Checked='<%#Eval("edited")%>' />
                    </EditItemTemplate>
                </asp:TemplateField>
            </Columns>
            <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
            <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
            <RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
            <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
            <SortedAscendingCellStyle BackColor="Azure" />
            <SortedAscendingHeaderStyle BackColor="Aqua" />
            <SortedDescendingCellStyle BackColor="Blue" />
            <SortedDescendingHeaderStyle BackColor="BlueViolet" />
        </asp:GridView>





One for the resume upload and one for picture upload.

When user updated the information and upload its resume and picture and hit in the upload button the row again come back to select mode and the respective cell in that row where picture has been uploaded shows the picture.



4. The picture must not save in the database or in physical path while uploading / editing.



Clarification:

1. I don’’t want to save the image in session. Because all of my experiment gone into vain so i decide to make an item template like this



One for the resume upload and one for picture upload.
When user updated the information and upload its resume and picture and hit in the upload button the row again come back to select mode and the respective cell in that row where picture has been uploaded shows the picture.

4. The picture must not save in the database or in physical path while uploading / editing.

Clarification:
1. I don''t want to save the image in session. Because all of my experiment gone into vain so i decide to make an item template like this

<asp:templatefield headertext="Student Picture" xmlns:asp="#unknown">
                    <itemtemplate>
                        <asp:image runat="server" id="student_Pic" imageurl="~/frmImagePrev.aspx" imagealign="Middle" alternatetext="Insert Picture"></asp:image>
                    </itemtemplate>
                    <edititemtemplate>
                        <asp:fileupload id="fileupload1" filename="<%# Bind("student_pic") %>" runat="server">
                        </asp:fileupload>
                    </edititemtemplate>
                </asp:templatefield>    





here



here

ImageUrl="~/frmImagePrev.aspx"

line redirect in the

line redirect in the

frmImagePrev.aspx

page and then it shows the picture in all the cells throughout the grid view.



Write now i am stuck here.



2. but this is my approach i need to achieve the goal not the approach.



I think i am clear now what i need. If more explanation needed i am happy to give u it full code is needed pls give me the mail id i can send it to you.

Thanking you : Tanmoy (INDIA)

page and then it shows the picture in all the cells throughout the grid view.

Write now i am stuck here.

2. but this is my approach i need to achieve the goal not the approach.

I think i am clear now what i need. If more explanation needed i am happy to give u it full code is needed pls give me the mail id i can send it to you.
Thanking you : Tanmoy (INDIA)