In the grid view I have uploaded files with image icon. For having image icon in the grid view and getting type of uploaded file in the row (command Argument), I have used image button, it shows the image icon but when I click on the image button, I have error" System.ArgumentException: Invalid postback or callback argument"
If I could use image control instead of image button for me was better, but I do not know how to get extension file type from row.
Also if we could disable link image button in this case, was no problem.
Desin of database for tblfile is like below:
Fileid(int)
Docid(int)
TransId(int)
Content type varchar(75)
Data varbinary(max)
Extention varchar(50)
Cs:
protected void GridViewEfile_DataBound(object sender, EventArgs e)
{
foreach (GridViewRow gvr in GridViewEfile.Rows) //loop each row
{
ImageButton ib = (ImageButton)gvr.FindControl("ib1"); //find imagebutton
string exten = ib.CommandArgument;; //get index of '.'
if (exten == ".pdf")
ib.ImageUrl = #"~/Images/iconpdf.gif"; //set image for imagebutton in each row
else if (exten == ".docx")
ib.ImageUrl = #"~/Images/icondoc.gif";
}
}
protected void ib1_Command(object sender, CommandEventArgs e)
{
_DataContext = new EDMSDataContext();
//you can get your command argument values as follows
string FileId = e.CommandName.ToString();
int _FileId = Convert.ToInt32(FileId);
tblFile Efile = (from ef in _DataContext.tblFiles
where ef.FileId == _FileId
select ef).Single();
if (Efile != null)
{
download(Efile);
}
}
.aspx:
<asp:GridView ID="GridViewEfile" runat="server" AutoGenerateColumns="False" CellPadding="4"
OnDataBound="GridViewEfile_DataBound" ForeColor="Black" DataKeyNames="FileID"
GridLines="None" ShowHeader="False">
<AlternatingRowStyle BackColor="Yellow" />
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:ImageButton runat="server" ID="ib1" OnCommand="ib1_Command" CommandName='<%#Eval("FileID")%>'
CommandArgument='<%# Eval("Exten") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" OnCommand="LinkButton1_Command" CommandName="Download"
CommandArgument='<%#Eval("FileID")%>'><%#Eval("FileName")%></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="ContentType" />
</Columns>
<EditRowStyle BackColor="#7C6F57" />
<FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="Red" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#E3EAEB" />
<SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
</asp:GridView>
Related
Please help with these ;
I have a gridview with AjaxFileupload in the Itemtemplate. After populating the gridview and uploading files using the AjaxFileUpload in each row. I need to read through the gridview and get the filename at bujtton click. Please find below the codes
<div>
<asp:GridView ID="grdView_Requirement" runat="server" AutoGenerateColumns="False" Font-Names="Segoe UI" Font-Size="12px">
<Columns>
<asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
<asp:TemplateField HeaderText="Upload File">
<ItemTemplate>
<asp:AjaxFileUpload ID="AjaxFileUploadNew" runat="server" Font-Names="Segoe
UI" Font-Size="12px" MaximumNumberOfFiles="1" Width="388px" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="">
</Columns>
<FooterStyle BackColor="#5D7B9D" ForeColor="#333333" />
<HeaderStyle BackColor="#D7D9E5" ForeColor="#333333" />
<RowStyle BackColor="#FFFFFF" ForeColor="#333333" />
</asp:GridView>
</div>
<asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
</asp:ToolkitScriptManager>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
Upload code
protected void AjaxFileUploadNew_UploadComplete(object sender, AjaxControlToolkit.AjaxFileUploadEventArgs e)
{
string strLongFilePath = e.FileName;
string strFileExtension = System.IO.Path.GetExtension(strLongFilePath);
char[] ch = new char[] { '\\' };
string[] strPath = strLongFilePath.Split(ch);
string strInternalFileName = DateTime.Now.ToFileTime().ToString() + strFileExtension;
string strDestPath = Server.MapPath("~/UploaderTemp/");
AjaxFileUploadNew.SaveAs(#strDestPath + strInternalFileName);
//Save path, filename to database here
}
Kindly help in getting the uploaded file name in the event below
protected void Button1_Click(object sender, EventArgs e)
{
foreach (GridViewRow row in grdView_Requirement.Rows)
{
//for bound field
string str = row.Cells[0].Text.ToString();
//for templated control
AjaxControlToolkit.AjaxFileUpload tb = row.FindControl("AjaxFileUploadNew") as
AjaxControlToolkit.AjaxFileUpload;
}
}
Kindly help resolve or give better ways of implementing to achieve success.
I am having one friend request form.In that,I have used grid view control to show all registered members,So one who want to send request he/she can send request.My form design code is:
enter code here
<asp:GridView ID="grdviewFriendRequest" runat="server" AllowPaging="true" AutoGenerateColumns="false"
EmptyDataText="No members found" Width="100%" Font-Size="16px" ForeColor="#FF9F00"
GridLines="None" PagerSettings-Mode="NumericFirstLast"
onselectedindexchanged="grdviewFriendRequest_SelectedIndexChanged">
<FooterStyle BackColor="#FFB100" Font-Bold="true" ForeColor="#000000" />
<RowStyle BackColor="#FFD340" />
<EditRowStyle BackColor="#FFDE40" />
<SelectedRowStyle BackColor="#FFC900" Font-Bold="True" ForeColor="#000000" />
<PagerStyle BackColor="#FFEB73" ForeColor="Black" HorizontalAlign="Center" />
<HeaderStyle BackColor="#FFB100" Font-Bold="True" ForeColor="000000" HorizontalAlign="Left" />
<AlternatingRowStyle BackColor="#FFDF73" />
<Columns>
<asp:TemplateField HeaderText="First Name">
<ItemTemplate>
<%#Eval("firstName")%></ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Last Name">
<ItemTemplate>
<%#Eval("lastName")%></ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Send Friend Request">
<ItemTemplate>
<asp:Button ID="btnSendFrndReq" CssClass="btnImage" Text="Send Friend Request" runat="server" OnClick="btnSendFrndReq_Click" /></ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
So,I want to do like,when user click on button in front of particular member's name,the data from that row should get store in a table.
I have tried this on button click event ,but it's not working.I am unable to find what is going wrong.I giving the code behind,Please check it,and suggest me any solution
protected void grdviewFriendRequest_SelectedIndexChanged(object sender, EventArgs e)
{
}
public void btnSendFrndReq_Click(object sender, EventArgs e)
{
string fname = "", lname = "";
GridViewRow row = grdviewFriendRequest.SelectedRow;
fname = row.Cells[0].Text;
lname = row.Cells[1].Text;
Int32 frmCandiid = (Int32)(Session["candiID"]);
InsertRegistation frndRq= new InsertRegistation();
Session["candID"] =frndRq.FrndReqGetCandiid(fname, lname);
Int32 toCandiid = (Int32)(Session["candID"]);
frndRq.InsertFrndReq(0, frmCandiid, toCandiid, DateTime.Now);
}
But after I try this i.e
enter code here
protected void grdviewFriendRequest_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "SendFriendReq")
{
int index = Convert.ToInt32(e.CommandArgument);
GridViewRow row = grdviewFriendRequest.Rows[index];
string fname = "", lname = "";
fname = row.Cells[0].Text;
lname = row.Cells[1].Text;
Int32 frmCandiid = (Int32)(Session["candiID"]);
InsertRegistation frndRq = new InsertRegistation();
Session["candID"] = frndRq.FrndReqGetCandiid(fname, lname);
Int32 toCandiid = (Int32)(Session["candID"]);
frndRq.InsertFrndReq(0, frmCandiid, toCandiid, DateTime.Now);
}
}
it gives me error, saying Input string not in a correct form.
then I try to give commandArgument property,but still it gives me value 0;
<asp:TemplateField HeaderText="Send Friend Request">
<ItemTemplate>
<asp:Button ID="btnSendFrndReq" CssClass="btnImage" Text="Send Friend Request" runat="server" CommandName="SendFriendReq" CommandArgument="<%# Container.DataItemIndex %>" /></ItemTemplate>
</asp:TemplateField>
and also tell me what value should be supply to command argument.
So,what I can do now?
It doesn't look like you actually need a command argument.
What you need is to find the row the clicked button was in.
And it happens you can just do this with the NamingContainer of the button (Control) propery.
var MyRow = (GridViewRow)((Control)sender).NamingContainer;
var usefullTextBox = (TextBox)MyRow.FindControl("usefullTextBox"); // Yes, the name is made up.
....
DoWhatYouWant(usefullTextBox.Value);
i'm new to asp.net. i'm doing one project. in that i've used one gridview and fetch datas from database using sqldatasource.
gridview code is
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataSourceID="Library" CellPadding="4" ForeColor="#333333"
GridLines="None" OnRowDataBound="GridView1_RowDataBound" >
<RowStyle BackColor="#EFF3FB" />
<Columns>
<asp:BoundField DataField="Sl_No" HeaderText="Sl_No" SortExpression="Sl_No" />
<asp:BoundField DataField="Title" HeaderText="Title" SortExpression="Title" />
<asp:BoundField DataField="Author" HeaderText="Author"
SortExpression="Author" />
<asp:BoundField DataField="Publication" HeaderText="Publication"
SortExpression="Publication" />
<asp:BoundField DataField="Available" HeaderText="Status"
SortExpression="Available" />
<asp:TemplateField HeaderText="Availability">
<ItemTemplate>
<asp:Image ID="Image1" runat="server" ImageUrl='<%# Eval("Available") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="RIUserTaken" HeaderText="RIUserTaken"
SortExpression="RIUserTaken" Visible="False" />
<asp:TemplateField HeaderText="Taken By" ShowHeader="False">
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%# Eval("RIUserTaken", "{0}") %>'></asp:Label>
<asp:Button ID="SendRequest" runat="server" Text="Button" Visible="False"
onclick="SendRequest_Click" CommandName="SendRequestCmd" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Taken Date" InsertVisible="False"
ShowHeader="False">
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Eval("TakenDate") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#2461BF" />
<AlternatingRowStyle BackColor="White" /> </asp:GridView>
code is one of the column of the grid view. if i click on that button, i've to store that button contains rows all datas in string variable. like
string slno="";
slno=gridview1.cells[0].text; (i'm not sure it is correct or not)
plz anyone help me??
thanks in advance :)
From msdn, you should check this.
GridView.SelectedIndexChanged event
void CustomersGridView_SelectedIndexChanged(Object sender, EventArgs e)
{
// Get the currently selected row using the SelectedRow property.
GridViewRow row = CustomersGridView.SelectedRow;
// Display the company name from the selected row.
// In this example, the third column (index 2) contains
// the company name.
MessageLabel.Text = "You selected " + row.Cells[2].Text + ".";
}
void CustomersGridView_SelectedIndexChanging(Object sender, GridViewSelectEventArgs e)
{
// Get the currently selected row. Because the SelectedIndexChanging event
// occurs before the select operation in the GridView control, the
// SelectedRow property cannot be used. Instead, use the Rows collection
// and the NewSelectedIndex property of the e argument passed to this
// event handler.
GridViewRow row = CustomersGridView.Rows[e.NewSelectedIndex];
// You can cancel the select operation by using the Cancel
// property. For this example, if the user selects a customer with
// the ID "ANATR", the select operation is canceled and an error message
// is displayed.
if (row.Cells[1].Text == "ANATR")
{
e.Cancel = true;
MessageLabel.Text = "You cannot select " + row.Cells[2].Text + ".";
}
}
Do these things
Step 1. Wire up a RowCommand Evnt to your GridView
Step 2. Inside that event at code-behind, do this
if(e.CommandName.Equals("SendRequestCmd"))
{
var clickedRow = ((Button)e.CommandSource).NamingContainer as GridViewRow;
// now access the cells like this
var clickedSLNo = clickedRow.cells[0].Text;
}
Update:
e.CommandSource definition
A instance of the System.Object class that represents the source of the command.
IButtonControl.CommandArgument definition
A control that implements the IButtonControl interface must implement the CommandArgument property and the CommandName property to indicate the argument and command name that are propagated to the Command event.
I have a web page with checkbox list to choose multiple crystal reports to view. I want to open these reports in new tabs or windows.
I tried this :
This is the show report button:
<asp:Button ID="Button2" runat="server" Text="Show Report" OnClick="ButtonShowReport_Click"
CssClass="button" OnClientClick="aspnetForm.target ='_blank';"/>
And this is my gridview control:
<asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333"
RowStyle-HorizontalAlign="Center" AutoGenerateColumns="False"
DataKeyNames="AccountID">
<RowStyle BackColor="#>
<Columns>
<asp:TemplateField HeaderText="Choose Client's Accounts">
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server"/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField Visible="false">
<ItemTemplate>
<asp:Label id="lblAccountId" runat ="server" text='<%# Eval("AccountID")%>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="AccountID" HeaderText="AccountID" Visible="false"/>
<asp:BoundField DataField="AccountName" HeaderText="Account Name" />
<asp:BoundField DataField="Name" HeaderText="Client Name" />
</Columns>
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#2461BF" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
My c# code looks like:
protected void ButtonShowReport_Click(object sender, EventArgs e)
{
string accountID;
int Rows = GridView1.Rows.Count;
for (int i = 0; i < Rows; i++)
{
//CheckBoxField cb = ((CheckBoxField)gvASH.Rows[i].Cells[1]).;
CheckBox cb = (CheckBox)GridView1.Rows[i].Cells[0].FindControl("CheckBox1");
if (cb.Checked == true)
{
accountID = ((Label)GridView1.Rows[i].FindControl("lblAccountID")).Text;//GridView1.DataKeys[i].Value.ToString();//GridView1.Rows[i].Cells[1].Text;
//Button2.Enabled = true;
openReport(accountID);
}
}
}
protected void openReport(string accountID)
{
//some code for setting parameters of the crystal report
//and for creating report key
Response.Redirect(string.Format("~/Report.aspx?ReportKey={0}",
ReportKey.ToString()),false);
}
So if the user checked 2 reports and clicked on the ShowReport button, it'll do the loop but opens only one new tab with the last report chosen ...
Any Suggestions please ??
Add this to the control which triggers the redirect. I know this works for Buttons/LinkButtons, not sure if it will work on a checkbox though.
OnClientClick="aspnetForm.target ='_blank';"
One method you might consider:
Instead of doing the Redirect to each report inside the code behind - dynamically generate some JavaScript that has uses window.open for each report. The javascript would look similar to this:
<script type="text/javascript">
window.open("<report1URL>", "_blank");
window.open("<report2URL>", "_blank");
...
window.open("<reportNURL>", "_blank");
</script>
Then, instead of doing a Response.Redirect, you should be able to do a Respone.Write() to get the browser to execute the code.
string myJavaScript= <dynamicallyGenerateJavascript>;
Response.Write(myJavaScript);
I have a Default.aspx page in which I have bind a Grid. In the Grid is a button named Details.
I also have a Details.aspx which has a GridView. If I click the button that exist in Default.aspx, there appears an Details.aspx page which appears empty.
When click the Detail button of a specific row, there appears the details of that button clicked. I am not understanding on how to pass the ID to Details.aspx.
Can anybody guide me to this please?
Details.aspx
<asp:GridView ID="DetailsGridView" runat="server" BackColor="White" AutoGenerateColumns="false"
BorderColor="#DEDFDE" BorderStyle="None" BorderWidth="1px" CellPadding="4" DataKeyNames="ID"
ForeColor="Black" GridLines="Vertical">
<FooterStyle BackColor="#CCCC99" />
<PagerStyle BackColor="#F7F7DE" ForeColor="Black" HorizontalAlign="Right" />
<SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
Default.aspx:
<Columns>
<asp:BoundField HeaderText="First Name" DataField="FirstName" />
<asp:BoundField HeaderText="Last Name" DataField = "LastName" />
<asp:BoundField HeaderText="HomePhoneNumber" DataField="HomePhoneNumber" />
<asp:TemplateField HeaderText="ViewDetails">
<ItemTemplate>
<asp:Button ID="Deatils" runat="server" Text="Details" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Actions">
<ItemTemplate>
<asp:Button ID="Modify" runat="server" Text="Modify" />
<asp:Button ID="Delete" runat="server" Text="Delete" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#CCCC99" />
<PagerStyle BackColor="#F7F7DE" ForeColor="Black" HorizontalAlign="Right" />
<SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
</div>
JavaScript
<script type="text/javascript">
function viewProfile(index)
{
var GridID = document.getElementById("PersonGridView");
var row=GridID.rows[parseInt(index)+1];
window.open('Details.aspx?coaid'+row);
}
</script>
Code Behind of Default.aspx:
protected void PersonGridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
var DataKeys = PersonGridView.DataKeys[e.Row.RowIndex];
DataKeys.Value.ToString();
var ID = DataKeys["ID"].ToString();
Button btnDetails = (Button)e.Row.FindControl("Deatils");
Button btnModify = (Button)e.Row.FindControl("Modify");
Button btnDelete = (Button)e.Row.FindControl("Delete");
btnModify.CommandName = "Modify";
btnDelete.CommandName = "Delete";
btnDetails.CommandName = "Deatils";
btnDelete.CommandArgument = btnModify.CommandArgument = btnDetails.CommandArgument = string.Format("{0}", ID);
btnDetails.Attributes["onclick"] = string.Format("viewProfile({0}); return false;", e.Row.RowIndex);
}
}
EDIT: I've modified my example to not use a HyperLink and instead will build a LinkButton that can navigate to your Details.aspx page passing the ID value in the QueryString:
Default.aspx:
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton CommandName="Details" CommandArgument='<%# Eval("ID") %>' Text="Details" runat="server" />
</ItemTemplate>
</asp:TemplateField>
Default.aspx.cs:
protected void PersonGridView_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Details")
{
Server.Transfer("Details.aspx?ID=" + e.CommandArgument.ToString());
}
}
On the Grid when you click the button
do the following
public protected sub Button_Click(EventArgs e)
{
if (grid.SelectedItem != null)
{
var selectedRow = grid.SelectedItem ;
// do what ever you want to Selected Row that may be binding it to another Form etc.
}
Do the same for OnSelectedEvent if you want to do it that way instead.
public protected sub OnSelectedEvent_Click(EventArgs e)
{
if (grid.SelectedItem != null)
{
var selectedRow = grid.SelectedItem ;
// do what ever you want to Selected Row that may be binding it to another Form etc.
}