ASP.NET GridView Image Command Button Problem (Multiple PostBacks) - asp.net

ASP.NET GridView Image Command Button causes Multiple PostBacks.Is this is a known issue ? or is there any solution(not workaround).

Try using this way
<asp:TemplateField>
<ItemTemplate>
<asp:ImageButton ID="Button" runat="server" ImageUrl="../images/Editicon.png" OnClick="Button_Click" />
</ItemTemplate>
</asp:TemplateField>
protected void Button_Click(object sender, EventArgs e)
{}
I m sure you wont face ne problem with this

Related

Making a form on asp.net

I'm having a problem making a button which would actually copy the information of the form and then displaying that information right below it! Please help me out in this.
I'm doing this in ASP.NET and trying to do in web forms!
Also the IDE I'm using is visual studio 2015
in asp.net if you want server side copy the information
<asp:TextBox runat="server" ID="TextBox1" />
<asp:Button ID="Button1" Text="show" runat="server" OnClick="Button1_Click" />
<asp:Label Text="result" ID="Lable1" runat="server" />
in behind
protected void Button1_Click(object sender, EventArgs e)
{
Lable1.Text = TextBox1.Text;
}

how to use images in the asp:gridview using asp.net?

How to use images in grid view using asp.net?
Actually i want use images in the place of text for example in the place of Edit and Delete i want use some images related to text. Is that possible to use images please help me
Please define your template like this way
<asp:TemplateField HeaderStyle-Width="40">
<ItemTemplate>
<asp:ImageButton ID="ButtonDelete" runat="server"
ImageUrl="~/Imags/delete.png" OnClick="ButtonDelete_Click" ToolTip="Delete"
CommandArgument='<%#Bind("UserId")%>'/>
</ItemTemplate>
</asp:TemplateField>
Code Behind
protected void ButtonDelete_Click(object sender, EventArgs e)
{
ImageButton button = sender as ImageButton;
DeleteUserById(Convert.ToInt32(button.CommandArgument));
}

Conditionally show hide asp linkbutton

I have below link button and I need to show it only if grid has records.
<asp:LinkButton ID="linkButton1" runat="server" ToolTip="Delete file" Visible='<%# (Convert.ToInt32(Eval("gridViewFileInformation.Rows.Count"))>0) %>' >Delete</asp:LinkButton>
But it shows always.
In your code behind file after databind of "gridViewFileInformation" you can set the Visible property of linkButton1. Similar question
Something like:
ASPX File:
<asp:GridView
runat="server"
ID="gridViewFileInformation"
OnDataBound="gridViewFileInformation_DataBound">
</asp:GridView>
CS File:
protected void gridViewFileInformation_DataBound(object sender, EventArgs e)
{
linkButton1.Visible = (gridViewFileInformation.Rows.Count > 0);
}

Dropdownlist disappear when put it in EditItemTemplate

If I put my dropdownlist column in ItemTemplate, it appear but I can't change the value. When I put it in EditItemTemplate like this:
<EditItemTemplate>
<asp:DropDownList DataValueField="COLUMN_NAME" DataTextField="COLUMN_NAME" DataSource='<%#GetDataSourceDesCol()%>' Width="90%" Visible=true ID="ddlDesCol" runat="server">
</asp:DropDownList>
</EditItemTemplate>
Then my ddl is not showing any more. How to fix it?
p/s: Even that I try with a new project and simple code like:
<asp:GridView ID="GridView1" AutoGenerateColumns=false runat="server">
<Columns>
<asp:TemplateField>
<EditItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server">
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
The ddl is not showing too!
All the controls inside the edit item template will be visible only when the grid view is in the edit mode.
So, you need to set the grid to edit mode. Inorder to make your code to work.
Hope this helps..
if you want to put entire grid view in edit mode:
protected void btnEdit_Click(object sender, EventArgs e)
{
GridView1.EditIndex = 1;
}
if you want a particular row to be in edit mode
Just implement the Row_Editing event and do something like this:
protected void Row_Editing(object sender, GridViewEditArgs e)
{
myGridView.EditItemIndex = e.EditItemIndex;
BindData();
}
Bind data will populate the GridView with the data.

Open new dynamic window within gridview dataview?

I have a gridview (called grdSearchResults) that contains links to pdfs. The pdf url's are returned from the database but I'm not sure how to add the url to an imagebutton control within the gridview.
Here's my codebehind:
List<SearchResults> search = _searchRepository.GetFactFileSearchResults(results);
grdSearchResults.DataSource = search;
grdSearchResults.DataBind();
And here's my aspx page code:
<asp:TemplateField HeaderText="FILE TYPE" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:ImageButton runat="server" ID="_imgbtnPreview" OnClientClick="<%# Container.DataItem("DownloadUrl") %>" ImageUrl="~/images/fileType_pdf.png" />
</ItemTemplate>
But this thows an error ("The server tag is not well formed") Any idea how to fix this? Thanks
I assume you want to call a javascript function OnClientClick. But you haven't showed us it so it's difficult to help.
You can also set the ImageButton's ImageUrl in RowDataBound (which i normally prefer):
protected void grdSearchResults_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DataRowView rv = (DataRowView) row.DataItem;
ImageButton imgbtnPreview = (ImageButton)e.Row.FindControl("_imgbtnPreview");
imgbtnPreview.ImageUrl = rv.Row.Field<String>("DownloadUrl");
}
}
Your OnClientClick should be using single quote in the outer
OnClientClick='<%# Container.DataItem("DownloadUrl") %>'
And the image can be
<asp:Image runat="server" ID="x" ImageUrl='<%# String.Format("~/{0}", Eval("ImageUrl")) %>' />

Resources