Edit gridview row asp.net without using commandfield - c#

I would like to make a column editable in a gridview but without the edit column (the one with the keys update, cancel ...) due to space problems. I tried to recall the javascript functions related to those buttons in the onRowDataBound but onclick event is not taken. Here my code. Can anyone help me please?
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.RowIndex == GridView1.EditIndex) //GridView is in edit mode
{
//update or cancel buttons
LinkButton updateBtn = (LinkButton)e.Row.Cells[0].Controls[0];
string updateScript = ClientScript.GetPostBackClientHyperlink(updateBtn, "");
Button1.Attributes["onclick"] = updateScript;
string cancelScript = string.Format("javascript:__doPostBack('{0}','Cancel${1}')", GridView1.ID, e.Row.RowIndex);
Button2.Attributes["onclick"] = cancelScript;
}
else //GridView is in read mode
{
//edit button
string editScript = string.Format("javascript:__doPostBack('{0}','Edit${1}')", GridView1.ID, e.Row.RowIndex);
e.Row.Attributes["onclick"] = editScript;
}
}
if (GridView1.EditIndex >= 0)
{
Button1.Enabled = true;
Button2.Enabled = true;
}
else
{
Button1.Enabled = false;
Button2.Enabled = false;
}
}
<asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1" AutoGenerateColumns="false" DataKeyNames="PKCOD" AllowPaging="True" PageSize="10"
CssClass="auto-style1" OnRowDataBound="GridView1_RowDataBound">
<Columns>
<%--
<asp:CommandField ShowEditButton="True" />
<asp:TemplateField HeaderText="QUANTITA" SortExpression="QUANTITA">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("QUANTITA") %>'></asp:TextBox>
<br />
<asp:RangeValidator ID="RangeValidator1" runat="server" ControlToValidate="TextBox1" EnableClientScript="False" ErrorMessage="ErrorMessage" MaximumValue="999" MinimumValue="0" Type="Integer">Valori tra 0 e 999</asp:RangeValidator>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("QUANTITA") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
--%>
<asp:CommandField ShowEditButton="True" />
<asp:TemplateField HeaderText="QUANTITA" SortExpression="QUANTITA">
<ItemTemplate>
<asp:TextBox ID="textBoxQuantita" runat="server" Text='<%# Bind("QUANTITA") %>'></asp:TextBox>
<asp:RangeValidator ID="RangeValidator1" Type="Double" MinimumValue="0" MaximumValue="999" ControlToValidate="textBoxQuantita" runat="server"
ErrorMessage="Inserisci solo numeri compresi tra 0 e 999"></asp:RangeValidator>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="CODICE" HeaderText="CODICE" ReadOnly="True" SortExpression="CODICE" />
<asp:BoundField DataField="DESCRIZIONE" HeaderText="DESCRIZIONE" ReadOnly="True" SortExpression="DESCRIZIONE" />
<asp:BoundField DataField="CATEGORIA" HeaderText="CATEGORIA" ReadOnly="True" SortExpression="CATEGORIA" Visible="False" />
<asp:BoundField DataField="PKCOD" HeaderText="PKCOD" ReadOnly="True" SortExpression="PKCOD" InsertVisible="False" Visible="False" />
<asp:BoundField DataField="USERNAME" HeaderText="USERNAME" ReadOnly="True" SortExpression="USERNAME" Visible="False" />
<asp:BoundField DataField="USERID" HeaderText="USERID" ReadOnly="True" SortExpression="USERID" Visible="False" />
<asp:BoundField DataField="USERDT" HeaderText="USERDT" ReadOnly="True" SortExpression="USERDT" Visible="False" />
<asp:BoundField DataField="STATO" HeaderText="STATO" ReadOnly="True" SortExpression="STATO" Visible="False" />
</Columns>
</asp:GridView>

Related

Update a SQL column if value is true

I have a bit value (in Status) i want to display its status in gridview as if true, the row display "Active", otherwise the row display "Inactive", this is my code, but the result displays True or false itself. And based on the Active or Inactive, the Action column must display Deactivate or Activate
ShopNumber ShopName Address Website Status Action Settings
2 abc Kuwait xyz.com True Deactivate Edit
3 def Kuwait deuuy False Activate Edit
4 Major Minor Usra.com True Activate Edit
5 Isys Kuwait isys.com False Activate Edit
6 Avenues Kuwait avenues.com False Activate Edit
Here is the codebehind:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Boolean bitCheck = Convert.ToBoolean(e.Row.Cells[4].Text);
if (bitCheck)
{
e.Row.Cells[4].Text = "Active";
}
else
{
e.Row.Cells[4].Text = "Inactive";
}
}
Here is ASP code:
<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
AutoGenerateColumns="False" DataSourceID="SqlDataSource1"
AllowSorting="True"
onselectedindexchanged="GridView1_SelectedIndexChanged"
DataKeyNames="TempID">
<Columns>
<asp:BoundField DataField="ShopNumber" HeaderText="ShopNumber" SortExpression="ShopNumber" >
<ItemStyle Width="150px"></ItemStyle>
</asp:BoundField>
<asp:BoundField DataField="ShopName" HeaderText="ShopName" SortExpression="ShopName" >
<ItemStyle Width="170px"></ItemStyle>
</asp:BoundField>
<asp:BoundField DataField="Address" HeaderText="Address" SortExpression="Address" >
<ItemStyle Width="170px"></ItemStyle>
</asp:BoundField>
<asp:BoundField DataField="Website" HeaderText="Website" SortExpression="Website" >
<ItemStyle Width="150px"></ItemStyle>
</asp:BoundField>
<asp:TemplateField HeaderText="Status" SortExpression="Status">
<EditItemTemplate>
<asp:TextBox ID="textStatus" runat="server" Text='<%# Bind("Status") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("Status") %>'></asp:Label>
</ItemTemplate>
<ItemStyle Width="150px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Action" SortExpression="Action">
<EditItemTemplate>
<asp:TextBox ID="textAction" runat="server" Text='<%# Bind("Action") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("Action") %>'></asp:Label>
</ItemTemplate>
<ItemStyle Width="150px" />
</asp:TemplateField>
<asp:CommandField HeaderText="Settings" ShowEditButton="True" >
<ItemStyle Width="150px"></ItemStyle>
</asp:CommandField>
</Columns>
</asp:GridView>
You are using item template, not just binding it to the column. So you'll have to cast the Label to edit it's Text.
Label status = (Label)e.Row.Cells[4].FindControl("Label2");
Label action = (Label)e.Row.Cells[5].FindControl("Label1");
Boolean bitCheck = Convert.ToBoolean(status.Text);
if (bitCheck)
{
status.Text = "Active";
action.Text = "Activated";
}
else
{
status.Text = "Inactive";
action.Text = "Deactivated";
}

binding image with Datagrid in asp.net not working well

I'm displaying information in a Datagrid not gridView in asp.net. the datagrid contain a column called Status. the status can either be 1 or -1. if the status is 1, i'm displaying a mark icon, then if the status is -1 then displayed a cancel icon. i have done the coding and its working but the problem is, the icons are not showing in all the column depending on the condition as shown in the image below. based on the value in my databasee, the status columns are all -1 but if its showing, its skip on column and bind the next column and so on.
here is my DataGrid Markup
<asp:DataGrid ID="VehicleInfo" runat ="server" AutoGenerateColumns="False" GridLines="Horizontal" Width="939px" CssClass=" table table-striped table-bordered" AllowCustomPaging="True" AllowPaging="True" PageSize="50" OnItemDataBound="VehicleInfo_ItemDataBound" OnItemCommand="VehicleInfo_ItemCommand" >
<Columns>
<asp:TemplateColumn HeaderText="#" >
<ItemTemplate>
<%# Container.DataSetIndex + 1 %>
</ItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn DataField="Type" HeaderText="Veh. Type"></asp:BoundColumn>
<asp:BoundColumn DataField="Make" HeaderText="Make/Model"></asp:BoundColumn>
<asp:TemplateColumn HeaderText="Veh. Reg No">
<ItemTemplate>
<asp:Label ID="RegNo" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.RegistrationNo") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn DataField="EngineNumber" HeaderText="Engine No"></asp:BoundColumn>
<asp:BoundColumn DataField="ChasisNo" HeaderText="Chasis No"></asp:BoundColumn>
<asp:BoundColumn DataField="InjectionYear" HeaderText="Injection Year"></asp:BoundColumn>
<asp:BoundColumn DataField="Size" HeaderText="Veh. Size"></asp:BoundColumn>
<asp:BoundColumn DataField="ServiceTypeName" HeaderText="Service Type"></asp:BoundColumn>
<asp:TemplateColumn HeaderText="Response Status">
<ItemTemplate>
<asp:Label ID="Status" Visible="false" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.registrationstatusResponse") %>'></asp:Label>
<asp:ImageButton ID="cancel" Width="30" Height="30" ImageUrl="/images/cancel.png" Runat=server Visible="false" />
<asp:ImageButton ID="good" Width="30" Height="30" ImageUrl="/images/good.png" Runat=server Visible="false" />
</ItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn DataField="verificationComment" HeaderText="Remark"></asp:BoundColumn>
</Columns>
<HeaderStyle Font-Bold="True" Font-Names="Verdana" Font-Size="X-Small" CssClass="" ForeColor="White" BackColor="#428BCA" Font-Italic="False" Font-Overline="False" Font-Strikeout="False" Font-Underline="False" />
<ItemStyle Font-Names="Verdana" Font-Size="X-Small" />
</asp:DataGrid>
and here is my code on the itemDataBound event and i Even try using a loop on the ItemDataBound event but it doesn't work. Please i need your help if there is anything im missing.
protected void VehicleInfo_ItemDataBound(object sender, DataGridItemEventArgs e)
{
if ((e.Item.ItemType == ListItemType.Item))
{
var lab1 = (Label)e.Item.FindControl("status");
var img1 = (ImageButton)e.Item.FindControl("good");
var img2 = (ImageButton)e.Item.FindControl("cancel");
if (lab1.Text == "1")
{
img2.Visible = false;
img1.Visible = true;
}
else if (lab1.Text == "-1")
{
img2.Visible = true;
img1.Visible = false;
}
}
}

Onselect in Dropdown send mail to respective user

I have a gridview in which I have a column name "Selection". I want whenver a admin selects Not Selected option the respective user should get an email on his ID that he has been rejected. Please see the gridview code for your reference:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" AllowPaging="true" Width="920"
PageSize="5" OnPageIndexChanging="gv_Applicants_PageIndexChanging" OnRowCommand="gv_Applicants_RowCommand"
EmptyDataText="No Applicants Found."
AllowSorting="true"
OnSorting="gv_Applicants_Sorting"
OnRowDataBound="gv_Applicants_RowDataBound" RowStyle-CssClass="a12" AlternatingRowStyle-CssClass="a22" ForeColor="#333333" GridLines="None" CssClass="table_box" HeaderStyle-Height="35px">
<AlternatingRowStyle BackColor="#F0F0F0" />
<Columns>
<asp:BoundField DataField="FirstName" HeaderText="First Name" HeaderStyle-Width="84" />
<asp:BoundField DataField="LastName" HeaderText="Last Name" HeaderStyle-Width="106" />
<asp:BoundField DataField="ContactNumber" HeaderText="Contact" HeaderStyle-Width="98" />
<asp:BoundField DataField="Email" HeaderText="Email" HeaderStyle-Width="150" />
<asp:TemplateField HeaderText="Position" SortExpression="Position" HeaderStyle-Width="107">
<ItemTemplate>
<%# Eval("Job.Position") %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Location" SortExpression="Location" HeaderStyle-Width="100">
<ItemTemplate>
<%# Eval("Job.Location") %>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="AppliedDate" DataFormatString="{0:MMMM dd, yyyy}" HeaderText="Date of Application" ReadOnly="true" HeaderStyle-Width="121" />
<asp:TemplateField HeaderText="Action" HeaderStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:LinkButton ID='lnkView' CommandName='v' Text='View' runat='server' CommandArgument='<%# Eval("ApplicantId") %>'></asp:LinkButton>
|
<asp:LinkButton ID='lnkdel' CommandName='d' Text='Delete' runat='server' CommandArgument='<%# Eval("ApplicantId") %>' OnClientClick="return confirm('Are you sure to delete?');"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Selection">
<ItemTemplate>
<asp:Label ID="lblCountry" runat="server" Visible="true" />
<asp:DropDownList ID="ddlCountries" runat="server">
<asp:ListItem Text="None" Value="1"></asp:ListItem>
<asp:ListItem Text="Selected" Value="2"></asp:ListItem>
<asp:ListItem Text="Not Selected" Value="3"></asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<EditRowStyle BackColor="#2461BF" />
<FooterStyle BackColor="#D8DADA" Font-Bold="True" />
<HeaderStyle BackColor="#D8DADA" Font-Bold="True" />
<PagerStyle BackColor="#D8DADA" HorizontalAlign="Center" />
<RowStyle BackColor="white" BorderStyle="Solid" BorderColor="#a8a8a8" BorderWidth="1px" Height="35" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
Eidted code:-
private void SendMail()
{
StringBuilder sbuilder = new StringBuilder();
sbuilder.Append("<div>");
sbuilder.Append("<p>Thanks for applying at RBL. You have been rejected</p>");
sbuilder.Append("</div>");
string str = sbuilder.ToString();
string ccEmail = "";
Common objCom = new Common();
string toId = ConfigurationManager.AppSettings["ddlEmail"].ToString();
objCom.SendEMail(toId, "Rejection Mail", sbuilder.ToString(), ccEmail, false, "");
}
protected void ddlSelection_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList ddl = (DropDownList)sender;
GridViewRow row = (GridViewRow)ddl.Parent.Parent;
if (ddl.SelectedValue == "Not Selected")
{
SendMail();
}
}
Try this,
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
DropDownList ddl = e.Row.FindControl("ddlCountries") as DropDownList;
if(ddl != null)
{
ddl.SelectedIndexChanged += new EventHandler(ddlCountries_SelectedIndexChanged);
}
}
}
protected void ddlCountries_SelectedIndexChanged(object sender, EventArgs e)
{
string selectedValue = ((DropDownList)sender).SelectedValue;
if(selectedValue== "Not Selected")
{
// Write code here for sending mail
}
}

How to disable LinkButton within GridViewclick from Client Side using JavaScript?

I have a GridView, In one column I have LinkButton control. I want to disable click from client side for certain condition on this column. Means for some rows it will not be possible for User to call onclick event and for some rows it is possible.
I want to achieve this from client side using javascript.
Or When User clicks on link, It will notify the User that this action can't be completed for this row.
<asp:GridView Width="100%" ShowHeader="true" ViewStateMode="Enabled" GridLines="Both" CellPadding="10" CellSpacing="5" ID="GridViewMultiplePOSAssociationId" runat="server" AllowSorting="false" AutoGenerateColumns="false" OnRowCommand="RewardGridMultiD_RowCommand"
AllowPaging="true" PageSize="8" OnRowDataBound="grdViewCustomers_OnRowDataBound" PagerSettings-Position="Top" PagerSettings-Mode="NumericFirstLast" PagerSettings-FirstPageText="First" PagerSettings-LastPageText="Last" DataKeyNames="POS Id">
<RowStyle CssClass="table_inner_text" BackColor="WhiteSmoke" BorderColor="CornflowerBlue" Wrap="true" ForeColor="Black" Height="30px" />
<HeaderStyle CssClass="table_head_text" />
<Columns>
<asp:TemplateField ItemStyle-Width="80px">
<ItemTemplate>
<a href="JavaScript:divexpandcollapse('div<%# Eval(" POS Id ") %>');">
<img alt="Details" id="imgdiv<%# Eval("POS Id") %>" src="images/plus.png" />
</a>
<div id="div<%# Eval(" POS Id ") %>" style="display: none;">
<asp:GridView ID="grdViewOrdersOfCustomer" runat="server" AutoGenerateColumns="false" CssClass="ChildGrid">
<RowStyle CssClass="table_inner_text" BackColor="SkyBlue" BorderColor="Black" Wrap="true" ForeColor="White" Height="30px" />
<Columns>
<asp:BoundField ItemStyle-Width="150px" DataField="RULE FILE NAME" HeaderText="RULE FILE NAME" />
<asp:BoundField ItemStyle-Width="150px" DataField="RULE ID" HeaderText="RULE ID" />
<asp:BoundField ItemStyle-Width="150px" DataField="RULE TYPE ID" HeaderText="RULE TYPE ID" />
<asp:BoundField ItemStyle-Width="150px" DataField="START TIME" HeaderText="START TIME" />
<asp:BoundField ItemStyle-Width="150px" DataField="EXPIRY TIME" HeaderText="EXPIRY TIME" />
</Columns>
</asp:GridView>
</div>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderStyle-Width="80px" ItemStyle-Width="80px" ItemStyle-HorizontalAlign="Center" HeaderText="Row Number">
<ItemTemplate>
<asp:Label ID="LabelRowNumberId" runat="server" Text='<%#Eval("Row Number") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderStyle-Width="80px" ItemStyle-Width="80px" ItemStyle-HorizontalAlign="Center" HeaderText="POS Id">
<ItemTemplate>
<asp:Label ID="LabelPOSID" runat="server" Text='<%#Eval("POS Id") %>'></asp:Label>
<%-- <asp:LinkButton ID="LinkbtnPOSId" CommandArgument='<%#Eval("POS Id") %>' CommandName="ClickPOS" Text='<%#Eval("POS Id") %>' runat="server" CausesValidation="false"></asp:LinkButton>--%>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderStyle-Width="80px" ItemStyle-Width="250px" ItemStyle-HorizontalAlign="Center" HeaderText="Action">
<ItemTemplate>
<asp:LinkButton ID="HyperLinkAssociate" CommandArgument='<%#Eval("POS Id") %>' CommandName="Associate" Text="Associate" runat="server" OnClientClick="return OnClientClickAssociateRewardRuleFile(this);" CausesValidation="false"></asp:LinkButton>/
<asp:LinkButton ID="HyperLinkReplace" CommandArgument='<%#Eval("POS Id") %>' CommandName="Replace" Text="Replace" runat="server" OnClientClick="return OnClientClickReplaceRewardRuleFile(this);" CausesValidation="false"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderStyle-Width="80px" ItemStyle-Width="250px" ItemStyle-HorizontalAlign="Center" HeaderText="Status">
<ItemTemplate>
<asp:Label runat="server" ID="LabelStatusPendingPOSId" Text='<%#Eval("Status") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
In GridViewMultiplePOSAssociationId there is one column "Status" which has label LabelStatusPendingPOSId, LabelStatusPendingPOSId text is set Applied, Not Applied at the time of Binding. For Rows which has Status Applied, User should not be able to click on LinkButton Associate/Replace else He is allowed to click.
use this code OnRowDataBound for Your Grid
protected void grdViewCustomers_OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType==DataControlRowType.DataRow)
{
LinkButton LinkbtnPOSId=(LinkButton)e.Row.FindControl("LinkbtnPOSId");
Label LabelStatusPendingPOSId = (Label)e.Row.FindControl("LabelStatusPendingPOSId");
Boolean boolStatus = LabelStatusPendingPOSId.Text == "Applied" ? true : false;
//LinkbtnPOSId.Attributes.Add("onclick", "function CheckStatus('" + boolStatus.ToString() + "')");
LinkbtnPOSId.Enabled = boolStatus;
}
}
Try this..
You can loop through the Gridview and make the particular control disable...
I am writing the code in javascript pageload function...
function PageLoad(sender, args)
{
for (var i = 0; i <= RowCount; i++)
{
document.getElementById('Gridview1_HyperLinkAssociate_' + i.toString() + '').disabled = true;
}
}
Set RowCount as the number of rows of the Gridview...

Value from row with DropDownList on DropDownList Index Changed Event

Code:
<asp:GridView ID="gv_Recruit" AutoGenerateColumns="False" Width="100%"
runat="server" OnRowCreated="gvStates_RowCreated"
OnRowDataBound="gvStates_RowCreated">
<Columns>
<asp:BoundField HeaderText="key" DataField="key" />
<asp:BoundField HeaderText="Name" DataField="Name" />
<asp:BoundField HeaderText="Quota" DataField="Quota" />
<asp:BoundField HeaderText="Session" DataField="Sess" >
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
<asp:TemplateField HeaderText="">
<ItemTemplate>
<asp:DropDownList ID="ddlCities" Width="100%" AutoPostBack="true" runat="server" OnSelectedIndexChanged="ddl_selected">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField HeaderText="Recruiter" DataField="Recruiter" />
<asp:BoundField HeaderText="MN Phone" DataField="MN Phone" />
<asp:BoundField HeaderText="Cell Phone" DataField="Cell Phone" />
</Columns>
</asp:GridView>
Code Behind:
protected void ddl_selected(object sender, EventArgs e)
{
string _dd_value = ((DropDownList)sender).SelectedValue;
string trying_to_get = gv_Recruit.Rows[???].Cells[0].Text.ToString();
string also_tried = gv_Recruit.SelectedRow.Cells[0].Text.ToString();
}
Basically what I'm trying to do is get the key value from the first row when the drop down is changed so I can perform an update ???
Can't figure this out.
Thanks in advance for the help...
Try
GridViewRow gRow = (GridViewRow)(sender as Control).Parent.Parent;
string trying_to_get = string.Empty;
if (gRow != null)
{
trying_to_get = gRow.Cells[0].Text;
}

Categories

Resources