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.
Related
I have 2 buttons, one is called "Accept", which is used for updating of database while another is called "View", which is used for Redirecting. However, in GridView's SelectedIndexChanged, i wasnt able to Validate which buttons were click
so I have tried Using RowCommand for the "View" button and SelectedIndexChanged for "Accept" button. But RowCommand is triggering the "Accept" button in the SelectedIndexChanged event handler.
<asp:GridView ID="QgridView" AutoGenerateColumns="False"
CssClass="table table-bordered" AllowPaging="True" PageSize="6"
BackColor="White" BorderColor="Black" BorderStyle="Solid"
ForeColor="Black" GridLines="None" runat="server"
OnRowCommand="QgridView_RowCommand"
OnSelectedIndexChanged="QgridView_SelectedIndexChanged" >
<Columns>
<asp:TemplateField HeaderText="No">
<ItemTemplate>
<span>
<%#Container.DataItemIndex + 1%>
</span>
</ItemTemplate>
</asp:TemplateField>
<asp:ImageField HeaderText="Image" DataImageUrlField="coverimg" >
<ControlStyle CssClass="coverimage"/>
<ItemStyle HorizontalAlign="Center" />
</asp:ImageField>
<asp:BoundField HeaderText="Buyer" DataField="buyer" />
<asp:BoundField HeaderText="Item" DataField="item" />
<asp:BoundField HeaderText="Price offered" DataField="price" />
<asp:buttonfield buttontype="Button"
commandname="Accept"
text="Accept"/>
<asp:BoundField DataField="quoteid" HeaderText="quoteid" ItemStyle-CssClass="hiddencol" HeaderStyle-CssClass="hiddencol" >
<HeaderStyle CssClass="hiddencol"></HeaderStyle>
<ItemStyle CssClass="hiddencol"></ItemStyle>
</asp:BoundField>
<asp:BoundField DataField="id" HeaderText="id" ItemStyle-CssClass="hiddencol" HeaderStyle-CssClass="hiddencol">
<HeaderStyle CssClass="hiddencol"></HeaderStyle>
<ItemStyle CssClass="hiddencol"></ItemStyle>
</asp:BoundField>
<asp:BoundField DataField="rtype" HeaderText="rtype" ItemStyle-
CssClass="hiddencol" HeaderStyle- CssClass="hiddencol">
<HeaderStyle CssClass="hiddencol"></HeaderStyle>
<ItemStyle CssClass="hiddencol"></ItemStyle>
</asp:BoundField>
<asp:BoundField DataField="seller" HeaderText="seller" ItemStyle-
CssClass="hiddencol" HeaderStyle-CssClass="hiddencol">
<HeaderStyle CssClass="hiddencol"></HeaderStyle>
<ItemStyle CssClass="hiddencol"></ItemStyle>
</asp:BoundField>
<asp:buttonfield buttontype="Button"
commandname="View"
text="View"/>
</Columns>
</asp:GridView>
// Behind COde page
protected void QgridView_SelectedIndexChanged(object sender, EventArgs e)
{
GridViewRow row = QgridView.SelectedRow;
string seller = row.Cells[10].Text;
string item = row.Cells[4].Text;
string type = row.Cells[9].Text;
int id = Convert.ToInt32(row.Cells[8].Text);
int quoteid = Convert.ToInt32(row.Cells[7].Text);
productDAO productdao = new productDAO();
productdao.GridPush(quoteid, id)
}
protected void QgridView_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "View")
{
Response.Redirect("ListingItems.aspx");
}
}
I want the "Accept" BUtton to perform the updating of database while the "View button to perform Response.Redirect. However, RowCommand does not fire and instead, it triggers the "Accept" button in SelectedIndexChanged. May i know how can perform the different task for each button?
[Gettting row data from GridView]
protected void QgridView_RowCommand(object sender,
GridViewCommandEventArgs e)
{
System.Diagnostics.Debug.WriteLine("onrowcommand");
if (e.CommandName == "View")
{
System.Diagnostics.Debug.WriteLine("ButtonView is clicked");
Response.Redirect("ListingItems.aspx");
}
else if (e.CommandName == "Accept")
{
System.Diagnostics.Debug.WriteLine("buttonAccept is clicked");
productDAO productdao = new productDAO();
//GridViewRow row = QgridView.SelectedRow;
int index = Convert.ToInt32(e.CommandArgument);
GridViewRow row = QgridView.Rows[index];
string id = row.Cells[8].Text; //Returns me nothing
}
else
{
System.Diagnostics.Debug.WriteLine("no command name");
}
}
I think you are no need to check the rowcommand or onselected Index change event you can directly set button on click event. if you are using the inside your Gridview, then you should be able to register an event in your button code as:
<asp:Button runat="server" OnClick="YourclickEvent" />
For more details on event you should read this: https://www.codeproject.com/Articles/50540/GridView-Event-Handling
One Addition: if using row command then identify each button with command name and you can perform your task easily.
According to your code SelectedIndexChanged should be never fired (no select command).
RowCommand handler always fire first (before more specific handlers).
Look at sample working code.
.aspx
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="CategoryID"
DataSourceID="sqlCategory"
OnSelectedIndexChanged="GridView1_SelectedIndexChanged"
OnRowCommand="GridView1_RowCommand" AllowPaging="True">
<Columns>
<asp:CommandField ShowSelectButton="True" />
<asp:BoundField DataField="CategoryID" HeaderText="CategoryID" InsertVisible="False" ReadOnly="True" SortExpression="CategoryID" />
<asp:BoundField DataField="CategoryName" HeaderText="Category Name" SortExpression="CategoryName" />
<asp:ButtonField ButtonType="Button" CommandName="Accept" HeaderText="Accept" ShowHeader="True" Text="Accept" />
<asp:ButtonField ButtonType="Button" CommandName="View" HeaderText="View" ShowHeader="True" Text="View" />
<%-- Use HyperLinkField instead of ButtonField --%>
<asp:HyperLinkField DataNavigateUrlFields="CategoryID" DataNavigateUrlFormatString="category.aspx?categoryid={0}" DataTextField="CategoryName" DataTextFormatString="View {0}" HeaderText="Direct View" />
<asp:CommandField ButtonType="Button" ShowEditButton="True" />
</Columns>
</asp:GridView>
<%-- Data from NorthWind learning DB --%>
<asp:SqlDataSource ID="sqlCategory" runat="server" ConnectionString="<%$ ConnectionStrings:DBConnection %>"
SelectCommand="SELECT CategoryID, CategoryName FROM Categories"
UpdateCommand="update categories set categoryName=#CategoryName where CategoryID=#CategoryID">
<UpdateParameters>
<asp:Parameter Name="CategoryID" Type="Int32" />
<asp:Parameter Name="CategoryName" Type="String" />
</UpdateParameters>
</asp:SqlDataSource>
.aspx.cs
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
Trace.Write("GridView1_SelectedIndexChanged");// fired by "select" command after RowCommand finished
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
Trace.Write("GridView1_RowCommand");
//
if (e.CommandName == "Accept")
{
var categoryId = (int)GridView1.DataKeys[Convert.ToInt32(e.CommandArgument)].Value;//e.CommandArgument is display index
Trace.Write(e.CommandName + ": " + e.CommandArgument + " : " + categoryId.ToString());
}
else if (e.CommandName == "View")
{
Trace.Write("View " + e.CommandArgument);
}
}
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);
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>
I have a GridView like so:
<asp:GridView runat="server" ID="grdPractices" PageSize="10" AutoGenerateColumns="False" DataKeyNames="Id"
CssClass="linkGrid" AllowSorting="True" OnSorting="grdPractices_OnSorting" OnRowDataBound="grdPractices_OnRowDataBound"
OnRowEditing="grdPractices_OnRowEditing" OnRowCancelingEdit="grdPractices_OnRowCancelingEdit" OnRowUpdating="grdPractices_OnRowUpdating">
<Columns>
<asp:BoundField DataField="Name" HeaderText="Name" meta:resourcekey="PracticeName" ReadOnly="True" SortExpression="Name" ItemStyle-Width="400px" />
<asp:BoundField DataField="Code" HeaderText="Code" meta:resourcekey="Code" ReadOnly="True" SortExpression="Code" ItemStyle-Width="200px" />
<asp:TemplateField meta:resourcekey="SiteName" ItemStyle-Width="200px" SortExpression="SiteName">
<ItemTemplate>
<asp:Literal runat="server" Text='<%# Eval("SiteName") %>' />
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList runat="server" ID="lstSites" />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField meta:resourcekey="NumOfUsers" ItemStyle-Width="200px" SortExpression="NumOfUsers">
<ItemTemplate>
<asp:LinkButton runat="server" OnCommand="OnLinkButtonCommand" CommandName="ViewUsers" Text='<%# Eval("NumOfUsers") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowDeleteButton="False" ShowCancelButton="True"
ShowInsertButton="False" ShowEditButton="True" EditText="Edit"
CancelText="Cancel" UpdateText="Update" meta:resourcekey="Edit" ItemStyle-Width="200px" />
</Columns>
</asp:GridView>
and here is the code for the OnRowEditing event:
protected void grdPractices_OnRowEditing(object sender, GridViewEditEventArgs e)
{
this.grdPractices.EditIndex = e.NewEditIndex;
var sitesDropDown = this.grdPractices.Rows[e.NewEditIndex].Controls[0].FindControl("lstSites") as DropDownList;
if (sitesDropDown == null)
{
return;
}
}
My problem is that I cannot get a hold of the lstSites control, which lives in the EditTemplate. I've tried using the following:
this.grdPractices.Rows[e.NewEditIndex].Controls[0].FindControl("lstSites")
as DropDownList;
this.grdPractices.Rows[e.NewEditIndex].FindControl("lstSites") as
DropDownList;
this.grdPractices.Rows[e.NewEditIndex].FindControlRecursive("lstSites")
as DropDownList;
The result is always the same, a NULL is returned.
How on earth are you supposed to get a control in a row when in the OnRowEditing event?
OK, what I was not doing, after this line:
this.grdPractices.EditIndex = e.NewEditIndex;
was then rebinding the grid's data. So after rebinding the data, and then calling:
var sitesDropDown = this.grdPractices.Rows[e.NewEditIndex].FindControlRecursive("lstSites") as DropDownList;
I am now able to interact with the siteDropDown variable since it now contains a reference to the lstSites control.
Try to use GridViewRow as following...
protected void grd_RowEditing(object sender, GridViewEditEventArgs e)
{
GridViewRow selectRow = grd.Rows(e.NewEditIndex);
DropDownList sitesDropDown =(DropDownList)selectRow.Cells[2].FindControl("lstSites");
}
I am trying to make a small gridview that lists the results of a query against a non standard SQL db, and with this information I want the user to be able to edit a column or two to update the data in the db. I know this is easy with an sqldatasource control but I lack that luxury and I am having trouble switching from my itemTemplates in the gridview to the edittemplates when I fire the gridview's edit event.
any pointers?
some code:
<asp:GridView ID="ExamEditGridView" runat="server" OnRowEditing="EditExam" OnRowUpdating="SaveEdit"
DataKeyNames="iproc_code" AllowSorting="true" AutoGenerateColumns="false" AutoGenerateEditButton="true">
<HeaderStyle BackColor="#006633" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#999999" />
<Columns>
<asp:TemplateField HeaderText="Exam Title">
<ItemTemplate>
<asp:Label ID="col1" runat="server" Text='<%# Bind("rpt_descrip") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="EditText" runat="server" Text='<%# Bind("rpt_descrip") %>' Visible="false" />
</EditItemTemplate>
</asp:TemplateField>
<asp:BoundField HeaderText="Short Description" DataField="short_descrip" />
<asp:BoundField HeaderText="Description" DataField="descrip" />
<asp:BoundField HeaderText="Sched Note" DataField="sched_note" />
</Columns>
</asp:GridView>
code behind:
(the query is just a simlpe select statement)
string mod_id = modSelect.SelectedValue;
string query = qry + mod_id;
using (OdbcConnection connection = new OdbcConnection(DbConnectionString))
{
OdbcDataAdapter adapter = new OdbcDataAdapter(query, connection);
DataTable dt = new DataTable();
connection.Open();
adapter.Fill(dt);
ExamEditGridView.DataSource = dt;
ExamEditGridView.DataBind();
}
the gridview is databound when a dropdown lists index changes, which gives me the mod_id for the query
I ended up getting it working, I didn't realize I had to set the gridview's editindex value and redatabind to toggle edit mode
code:
protected void EditExam(object sender, GridViewEditEventArgs e)
{
ExamEditGridView.EditIndex = e.NewEditIndex;
ExamEditGridView.DataSource = Session["data"];
ExamEditGridView.DataBind();
}
protected void CancelEdit(object sender, GridViewCancelEditEventArgs e)
{
ExamEditGridView.EditIndex = -1;
ExamEditGridView.DataSource = Session["data"];
ExamEditGridView.DataBind();
}