C#,asp.net gridview ,edit a value using the radiobutton - c#

I am using a grid in which inside the item template(the first one in which radio button is there). i use link button instead of radiobutton ,when i click on it iam able to edit as the textbox appears, but if i use the radio button,that editing option is not coming;
aspx:
<div id="loginblock">
<table>
<tr>
<td>UserName:</td>
<td>
<asp:TextBox ID="username" runat="server" onBlur="txtvalidation();" AutoPostBack="true"></asp:TextBox>
</td>
<td>
<div id="warning" style="color:aqua;width:100px" runat="server">
</div>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="PlS Enter Password" ControlToValidate="pwd"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td>Password:</td>
<td>
<asp:TextBox ID="pwd" runat="server" TextMode="Password" ></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Button ID="btnLogin" runat="server" OnClick="btnLogin_Click" Text="Button" />
</td>
</tr>
<tr>
<asp:GridView ID="grd" runat="server" OnRowEditing="selectrd_CheckedChanged" AutoGenerateColumns="false" DataKeyNames="userid">
<Columns>
<asp:TemplateField HeaderText="edition">
<ItemTemplate>
<asp:RadioButton ID="rdnselect" runat="server" CommndName="edit" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField HeaderText="ids" DataField="userid" />
<asp:TemplateField HeaderText="password">
<ItemTemplate>
<%# Eval("pwd") %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="edits" runat="server" Text='<%# Eval("pwd") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
cs:
protected void selectrd_CheckedChanged(object sender, GridViewEditEventArgs e)
{
grd.SelectedIndex = e.NewEditIndex;
ldgrid();
}
Here if we remove the radiobutton and insert link button,onclick of the link button we can edit the values,pls say me how to do the same operation with the radio button.

Related

Get textbox value in repeater control when button in same row is clicked

I have this repeater:
<asp:Repeater ID="rptImages" runat="server">
<HeaderTemplate>
<table>
<tr>
<th>Image</th>
<th>Caption</th>
<th> </th>
<th>Update File</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<asp:Image Width="108px" Height="67px" runat="server" ID="imgDb" ImageUrl='<%# Eval("imageUrl") %>' />
</td>
<td>
<asp:TextBox runat="server" style="margin-left: 5px; margin-right: 5px;" ID="txtCaption" Text='<%# Eval("caption") %>'></asp:TextBox>
</td>
<td>
<td><asp:FileUpload ID="fu" runat="server" /></td>
<td><asp:Button ID="btn" runat="server" Text="Update" CssClass="btn btn-info" CommandArgument='<%# Eval("id") %>' OnClick="btn_OnClick" /></td>
<td><asp:Button runat="server" ID="btnDelete" CssClass="btn btn-danger" CommandArgument='<%# Eval("id") %>' OnClick="btnDelete_OnClick" Text="Delete" /></td>
<td><asp:HiddenField ID="lblC" runat="server" Value='<%# DataBinder.Eval(Container.DataItem, "id") %>'></asp:HiddenField></td>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
I want to know how can I get the textbox specifically from the row where the update button is clicked. What logic should I use to get the value I need?
You could probably do that with handling Click event on the button and then getting its parent control. But I would recommend handling command of the repeater instead:
<asp:Repeater ID="rptImages" runat="server"
OnItemCommand="rptImages_ItemCommand">
Remove the handler from the button, but keep the command arg. You may also want to set command name if there are other controls sending commands from within the repeater:
<asp:Button runat="server" ID="btnDelete" CssClass="btn btn-danger"
CommandArgument='<%# Eval("id") %>'
Text="Delete" />
And in the code behind this becomes trivial:
protected void rptImages_ItemCommand(object sender, RepeaterCommandEventArgs e)
{
TextBox t = e.Item.FindControl("txtCaption") as TextBox;
}

linkbutton or button onClick not fireing inside gridview ItemTemplate

hi i have a gridview in my asp.net webpage that is bounded after a user selects 3 dropdown list.
dropdown list's auto postbacks are set true , now my problem is when i put a button , imagebutton or linkbutton in my gridview ItemTemplate the onclick event dosent fire !
here is my code
<asp:GridView ID="GridView1" runat="server" Width="90%" Height="100%" OnRowCommand="GridView1_RowCommand" OnPageIndexChanging="GridView1_PageIndexChanging" style="margin:20px; vertical-align:top;" PageSize="10" AllowPaging="True"
AllowSorting="True"
AutoGenerateColumns="False" DataKeyNames="WFStudentID" ShowHeader="true" BorderWidth="1px">
<Columns>
<asp:TemplateField ShowHeader="true">
<ItemTemplate >
<tr>
<td>
<asp:Label ID="Label1" ForeColor="Silver" Font-Size="Medium" runat="server" Text='<%# Bind("Name") %>'></asp:Label>
</td>
<td>
<asp:Label ID="Label2" runat="server" ForeColor="Silver" Font-Size="Medium" Text='<%# Bind("Family") %>'></asp:Label>
</td>
<td>
<asp:HyperLink ID="HyperLink1" ForeColor="Silver" Font-Size="Medium" Target="_blank" NavigateUrl='<%# Bind("WFStudentFilePath") %>' runat="server">دانلود</asp:HyperLink>
</td>
<td>
<asp:Label ID="Label7" runat="server" ForeColor="Silver" Font-Size="Medium" Text='<%# Bind("WFStudentDate") %>'></asp:Label>
</td>
<td>
<asp:Button ID="Deny" Text="deny" OnClick="Deny_Click1" runat="server" />
</td>
<td>
<asp:ImageButton ID="accept" OnClick="accept_Click" runat="server" />
</td>
<td>
<asp:TextBox ID="des" TextMode="MultiLine" Height="100px" Width="200px" runat="server"></asp:TextBox>
</td>
</tr>
<br />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
protected void Deny_Click1(object sender, EventArgs e)
{
Response.Redirect("home.aspx");
}
It is probably firing, but it will throw an exception (which your catch block ignores). The exception is because of this line:
LinkButton Link = (LinkButton)sender;
The sender is a Button, not a LinkButton, so the cast is not valid and an exception will be thrown.
you can check this::
http://www.dotnetbull.com/2013/05/how-to-handle-click-event-of-linkbutton.html
verify your code like this
Buttons inside GridView do not respond to direct events. They have to go through GridView's RowCommand event implementation. I've seen that you have implemented OnRowCommand="GridView1_RowCommand". So now you need make following change to image button:
<asp:ImageButton ID="accept" runat="server" CommandName="Accept" />
Now look for "Accept" command name in your GridView1_RowCommand event handler.
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Accept")
{
// Your code goes here
}
}
I hope this helps.

How to run SelectCommand in <asp:SqlDataSource> tag

I am making QueryString parameter ,so I have two .aspx forms. One form Default.aspx contains three fields SId, FirstName, LastName. when I submit the values, those values are saved in database.Now I want show this data on grid for particular SId on default2.aspx. Grid takes data through the SqlDataSource.SO I want to call the SqlDataSource on button click and grid should show data for particular SId on default2.aspx.
Default.aspx :
<body>
<form id="form1" runat="server">
<div>
</div>
<table cellpadding="2" cellspacing="5">
<tr>
<td>
<asp:Label ID="lblId" runat="server" Text="SId"></asp:Label>
</td>
<td>
<asp:TextBox ID="tbid" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Label ID="lblname" runat="server" Text="FirstName"></asp:Label>
</td>
<td>
<asp:TextBox ID="tbfirstname" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Label ID="lbllastname" runat="server" Text="LastName"></asp:Label>
</td>
<td>
<asp:TextBox ID="tblastname" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
</td>
<td>
<asp:Button ID="btnsubmit" runat="server" Text="Submit"
onclick="btnsubmit_Click1" Width="102px" />
</td>
</tr>
</table>
<br />
</form>
</body>
Default2.aspx :
<body>
<form id="form1" runat="server">
<div>
<table cellpadding="2" cellspacing="5">
<tr>
<td>
SId</td>
<td>
<asp:TextBox ID="tbId" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
</td>
<td>
<asp:Button ID="btnSearch" runat="server" Text="Search"
onclick="btnSearch_Click" />
</td>
</tr>
</table>
</div>
<br />
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
AutoGenerateSelectButton="True"
DataSourceID="SqlDataSource1" EnableModelValidation="True"
onrowcommand="GridView1_RowCommand">
<Columns>
<asp:TemplateField HeaderText="SId">
<ItemTemplate>
<asp:Label ID="lblSId" runat="server" Text='<%# Bind("SId") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="FirstName">
<ItemTemplate>
<asp:Label ID="lblFirstName" runat="server" Text='<%# Bind("FirstName")%>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="LastName">
<ItemTemplate>
<asp:Label ID="lblLastName" runat="server" Text='<%# Bind("LastName")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:testConnectionString %>"
SelectCommand="SELECT * FROM [Student] where SId = #tbId">
<SelectParameters>
<asp:QueryStringParameter Name="SId" QueryStringField="SId" />
</SelectParameters>
</asp:SqlDataSource>
</form>
</body>
I want to call the SqlDataSource1 on button click like..
Default2.aspx.cs :
protected void btnSearch_Click(object sender, EventArgs e)
{
string SId = tbId.Text;
SqlDataSource1.DataBind();
}
You don't need the button click. The SqlDataSource executes on every post back. The search button is going to issue a post back. But you are going to need to change a few things. First you need to get your parameters right, so remove this one:
<asp:QueryStringParameter Name="SId" QueryStringField="SId" />
and put this one in its place:
<asp:ControlParameter ControlID="tbId" PropertyName="Text" Name="tbId" />
and now remove all of this code:
protected void btnSearch_Click(object sender, EventArgs e)
{
string SId = tbId.Text;
SqlDataSource1.DataBind();
}
and now the SqlDataSource is tied directly to the control value, so when the user puts in a new control value and clicks the button it will be applied and new data retrieved without any of your interaction.

Format Decimal number to Indian Currency Format

I am using C# asp.net 4.0 in my project where i need to display price in India curreny format.
eg. my number is 12550000.00 then i want to display it as 1,25,50,000.00
But i want this to be displayed in gridview when i bind the data to the gridview,
so it can be done in markup page. where we place Eval for each Item Data Bound.
However, i would also like to explain my senario for displaying comma sepearted values.
i have a set of textboxes above the gridview where user makes entries of values and click add.
this gets add in the viewstate and the viewstate is binded to gridview.
In gridview i also have Edit button on click of it the values in viewstate is passed back to textbox on RowCommand Event of gridview. and on update click the viewstate datatable is updated and Binded back to gridview.
FOR your reference:
protected void gvPropertyConfig_RowCommand(object sender, GridViewCommandEventArgs e)
{
try
{
if (e.CommandName == "EditItem")
{
int index = Convert.ToInt32(e.CommandArgument);
hdnIndex.Value = index.ToString();
DataTable dt = (DataTable)ViewState["proeprtyConfig"];
DataRow dr = dt.Rows[index];
if (Request.QueryString["CMD"] == "Edit")
{
hdnPropertyConfigID.Value = dr["config_id"].ToString();
if (dr["is_active"].ToString().ToLower() == "true")
{
chkConfigVisible.Checked = true;
}
else
{
chkConfigVisible.Checked = false;
}
thIsActHed.Visible = true;
tdIsActchk.Visible = true;
tdbtnConfig.ColSpan = 2;
}
txtScalableArea.Text = dr["scalable_area"].ToString();
txtCarpetArea.Text = dr["carpet_area"].ToString();
txtPricePerSqFt.Text = dr["price_per_sq_ft"].ToString();
txtCCPricePerSqFt.Text = dr["cc_price_per_sq_ft"].ToString();
txtTotalPrice.Text = dr["total_price"].ToString();
ddlNoOfBedrooms.SelectedValue = dr["room_id"].ToString();
btnUpdateConfig.Visible = true;
btnConfigSubmit.Visible = false;
}
if (e.CommandName == "DeleteItem")
{
int index = Convert.ToInt32(e.CommandArgument);
DataTable dt = (DataTable)ViewState["proeprtyConfig"];
DataRow dr = dt.Rows[index];
if (Request.QueryString["CMD"].ToString() == "Edit")
{
int PropertyConfigID = Convert.ToInt32(dr[0].ToString());
prConfigObj.deletePropertyConfig(PropertyConfigID);
fillData();
}
else
{
dr.Delete();
gvPropertyConfig.DataSource = (DataTable)ViewState["proeprtyConfig"];
gvPropertyConfig.DataBind();
}
clearConfigTextBoxes();
btnConfigSubmit.Visible = true;
btnUpdateConfig.Visible = false;
}
setChecklistAttr();
}
catch (Exception ex)
{
throw ex;
}
}
Below is the markup for Gridview,
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<div class="tabBord">
<table>
<tr>
<td colspan="4" class="middle">
<h4>
Property Config Information</h4>
</td>
</tr>
<tr>
<td colspan="4">
<p>
Note: Enter total prices in lacs only. Eg. If 1 Crore than enter 1,00,00,000
</p>
<p>
</p>
</td>
</tr>
<tr>
<td>
<div id="divconfigstr" runat="server">
Configuration<span style="color: Red">*</span></div>
<%--class="displaynon"--%>
</td>
<td>
<div id="divnoofbedrooms" runat="server">
<asp:DropDownList Enabled="false" ID="ddlNoOfBedrooms" runat="server">
</asp:DropDownList>
<p>
</p>
</div>
</td>
<td>
Scalable Area <span style="color: Red">*</span>
</td>
<td>
<asp:TextBox ID="txtScalableArea" runat="server" autocomplete="off" MaxLength="10"></asp:TextBox><p>
</p>
</td>
</tr>
<tr>
<td>
Carpet Area <span style="color: Red">*</span>
</td>
<td>
<asp:TextBox ID="txtCarpetArea" runat="server" autocomplete="off" MaxLength="10"></asp:TextBox><p>
</p>
</td>
<td>
Price/Sq.Ft.<span style="color: Red">*</span>
</td>
<td>
<asp:TextBox ID="txtPricePerSqFt" runat="server" autocomplete="off" MaxLength="10"></asp:TextBox><p>
</p>
</td>
</tr>
<tr>
<td>
CC Price/Sq.Ft.<span style="color: Red">*</span>
</td>
<td>
<asp:TextBox ID="txtCCPricePerSqFt" runat="server" autocomplete="off" MaxLength="10"></asp:TextBox><p>
</p>
</td>
<td>
Total Price (in lacs)<span style="color: Red">*</span>
</td>
<td>
<asp:TextBox ID="txtTotalPrice" runat="server" autocomplete="off" MaxLength="10"></asp:TextBox><p>
</p>
</td>
</tr>
<tr>
<td id="thIsActHed" runat="server">
Active
<asp:HiddenField ID="hdnPropertyConfigID" runat="server" />
<asp:HiddenField ID="hdnIndex" runat="server" />
</td>
<td id="tdIsActchk" runat="server">
<asp:CheckBox ID="chkConfigVisible" runat="server" CssClass="checklist" /><p>
</p>
</td>
<td id="tdbtnConfig" runat="server" colspan="2">
<div class="btnHold">
<asp:Button ID="btnConfigSubmit" runat="server" Text="Add" OnClientClick="return ValidatePropertyConfig();"
CssClass="sendBtn" OnClick="btnConfigSubmit_Click" />
<asp:Button ID="btnUpdateConfig" runat="server" OnClick="btnUpdateConfig_Click" OnClientClick="return ValidatePropertyConfig();"
CssClass="sendBtn" Text="Update" Visible="False" />
<asp:Label ID="lblerrconfig" CssClass="errormsg" runat="server"></asp:Label>
</div>
</td>
</tr>
<tr>
<td colspan="4">
<div class="pHold">
<div class="gridH">
<asp:GridView ID="gvPropertyConfig" runat="server" AutoGenerateColumns="False" OnRowCommand="gvPropertyConfig_RowCommand"
OnRowDataBound="gvPropertyConfig_RowDataBound">
<Columns>
<asp:TemplateField HeaderText="No Of Bedrooms" ItemStyle-CssClass="txtLT">
<ItemTemplate>
<asp:Label ID="lblno_of_bedrooms" runat="server" Text='<%# Eval("room_no") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Scalable Area" ItemStyle-CssClass="txtRT">
<EditItemTemplate>
<asp:TextBox ID="txtscalable_area" runat="server" Text='<%# Eval("scalable_area") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblscalable_area" runat="server" Text='<%# Eval("scalable_area") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Carpet Area" ItemStyle-CssClass="txtRT">
<EditItemTemplate>
<asp:TextBox ID="txtcarpet_area" runat="server" Text='<%# Eval("carpet_area") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblcarpet_area" runat="server" Text='<%# Eval("carpet_area") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Price/SqFt." ItemStyle-CssClass="txtRT">
<EditItemTemplate>
<asp:TextBox ID="txtprice_per_sq_ft" runat="server" Text='<%# Eval("price_per_sq_ft") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblprice_per_sq_ft" runat="server" Text='<%# Eval("price_per_sq_ft") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="CC Price/SqFt." ItemStyle-CssClass="txtRT">
<EditItemTemplate>
<asp:TextBox ID="txtcc_price_per_sq_ft" runat="server" Text='<%# Eval("cc_price_per_sq_ft") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblcc_price_per_sq_ft" runat="server" Text='<%# Eval("cc_price_per_sq_ft") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Total Price (in lacs)" ItemStyle-CssClass="txtRT">
<EditItemTemplate>
<asp:TextBox ID="txttotal_price" runat="server" Text='<%# Eval("total_price") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lbltotal_price" runat="server" Text='<%# Eval("total_price") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="" ItemStyle-CssClass="txtLT">
<ItemTemplate>
<asp:ImageButton runat="server" ID="btnEditItem" CssClass="edBtn" ImageUrl="~/Admin/Includes/Images/edit.png"
ToolTip="Edit Item" CommandName="EditItem" CommandArgument="<%# ((GridViewRow) Container).RowIndex %>" />
<asp:ImageButton runat="server" ID="btnDeletetem" CssClass="edBtn" ImageUrl="~/Admin/Includes/Images/delete.png"
CommandName="DeleteItem" ToolTip="Delete Item" CommandArgument="<%# ((GridViewRow) Container).RowIndex %>" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</div>
</td>
</tr>
</table>
</div>
</ContentTemplate>
</asp:UpdatePanel>
Use the "C" parameter in the ToString function, and make sure you set the globalaztion attributes.
string parseValueIntoCurrency(double number) {
// set currency format
string curCulture = Thread.CurrentThread.CurrentCulture.ToString();
System.Globalization.NumberFormatInfo currencyFormat = new
System.Globalization.CultureInfo(curCulture).NumberFormat;
currencyFormat.CurrencyNegativePattern = 1;
return number.ToString("c", currencyFormat);
}
If you want to use a different Culture (say you're in USA, and you want the Indian format) then just use the appropriate CultureInfo element rather than getting it out of the current thread.
EXTRA INFO DUE TO OP EDIT
All right, what you're wanting to do to get this into your grid, is to create a PROTECTED function which takes in the number to be converted, and returns the converted string (this is basically the code above.
Now, on the ASPX side, you need to use that function in your grid view.
So, instead of this:
<asp:TemplateField HeaderText="Total Price (in lacs)" >
<EditItemTemplate>
<asp:TextBox ID="txttotal_price" runat="server"
Text='<%# Eval("total_price") %>' />
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lbltotal_price" runat="server"
Text='<%# Eval("total_price") %>'> />
</ItemTemplate>
</asp:TemplateField>
you'll use this templatefield:
<asp:TemplateField HeaderText="Total Price (in lacs)" >
<EditItemTemplate>
<asp:TextBox ID="txttotal_price" runat="server"
Text='<%# Eval("total_price") %>' />
</EditItemTemplate>
<ItemTemplate>
<%# parseValueIntoCurrency(Eval("total_price")) %>'>
</ItemTemplate>
</asp:TemplateField>
Note, two things. The first is that I'm still passing the UNFORMATTED value into the EDIT TEMPLATE, and that I'm not instantiating an extra LABEL in the ITEM TEMPLATE.
The reason I'm not doing the extra label, is because we just don't need it in there. That's just a bit more processor/memory overhead that you just don't need to incur.
As for passing the unformatted value to the text field, that's because it'll ultimately be easier to validate without having to parse out the commas and other string elements.

How to hookup the LinkButton in a GridView with a ModalPopupExtender in ASP.Net C#?

I really need some help here. I am trying to hookup a linkbutton in my GridView to a ModalPopupExtender, but with no luck. Basically I have a GridView which list all the users' info from a database, and I made the username as linkbutton. When you click the username, a modalpopup should show up, and you should be able to edit the user and update the database. I also I have an Add button. when you click the button, the same modalpopup should show up and you can add a new user to the database.
Following are my aspx and code behind. So far I have two major problems.
(1) the OnClick even doesn't get fired all the time.
(2) the modalpopup doesn't show up when clicking the username linkbutton.
Really appreciated if someone could help me out here.
Here is my aspx page:
<%# Page Title="" Language="C#" MasterPageFile="~/Default.Master" AutoEventWireup="true" CodeBehind="EditUsers.aspx.cs" Inherits="SPR2_v1.EditUsers" %>
<%# Register assembly="AjaxControlToolkit" namespace="AjaxControlToolkit" tagprefix="asp" %>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<div id="pagetitle">
<h1>SPR Users</h1>
</div>
<div >
<asp:Label ID="lblErrorMsg" runat="server" ForeColor="Red"></asp:Label>
<table align="center" >
<tr>
<td align="right">
<asp:Button ID="btnAddUser" runat="server" Text="Add User" Width="85px"
onclick="btnAddUser_Click" />
</td>
</tr>
<tr>
<td align="left">
<asp:GridView ID="gvUsers" runat="server" AllowPaging="True"
AllowSorting="True" AutoGenerateColumns="False"
DataSourceID="SqlDSUsers" EnableViewState="False">
<Columns>
<asp:TemplateField HeaderText="UserName">
<ItemTemplate>
<asp:LinkButton ID="lnkUserName" runat="server"
Text='<%# Eval("UserName")%>' OnClick="lnkUserName_Click">
</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="UserName" HeaderText="UserName"
SortExpression="UserName" Visible="false" />
<asp:BoundField DataField="Extension" HeaderText="Extension"
SortExpression="Extension" />
<asp:BoundField DataField="Email" HeaderText="E-mail"
SortExpression="Email" />
<asp:BoundField DataField="Company" HeaderText="Company"
SortExpression="Company" />
<asp:BoundField DataField="Department" HeaderText="Department"
SortExpression="Department" />
<asp:BoundField DataField="Access Level" HeaderText="Access Level"
SortExpression="Access Level" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDSUsers" runat="server"
ConnectionString="<%$ ConnectionStrings:SPRConnectionString %>"
SelectCommand="..."></asp:SqlDataSource>
</td>
</tr>
</table>
<asp:ModalPopupExtender id="mpeAddUser" runat="server"
TargetControlID="btnAddUser"
PopupControlID="panelEditUser"
CancelControlID="btnCancel"
PopupDragHandleControlID="PopupHeader"
Drag="true"
DropShadow="true"
BackgroundCssClass="ModalPopupBG" >
</asp:ModalPopupExtender>
<asp:Button ID="btnEditUser" runat="server" style="display:none" />
<asp:ModalPopupExtender id="mpeEditUser" runat="server"
TargetControlID="btnEditUser"
PopupControlID="panelEditUser"
CancelControlID="btnCancel"
PopupDragHandleControlID="PopupHeader"
Drag="true"
DropShadow="true"
BackgroundCssClass="ModalPopupBG" >
</asp:ModalPopupExtender>
<asp:panel id="panelEditUser" style="display: none" runat="server">
<div id="ModalPopup">
<div id="PopupHeader">Add a New User</div>
<table>
<tr>
<td>First Name</td>
<td>
<asp:TextBox ID="txtFirstName" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>Last Name</td>
<td>
<asp:TextBox ID="txtLastName" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>Extension</td>
<td>
<asp:TextBox ID="txtExtension" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>E-mail</td>
<td>
<asp:TextBox ID="txtEmail" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>Company</td>
<td>
<asp:DropDownList ID="ddlCompany" runat="server" DataSourceID="SqlDSCompany"
DataTextField="ProductVendorName" DataValueField="ProductVendorID"
AppendDataBoundItems="True">
<asp:ListItem Value="" Text=""></asp:ListItem>
</asp:DropDownList>
<asp:SqlDataSource ...>
</asp:SqlDataSource>
</td>
</tr>
<tr>
<td>Department</td>
<td>
<asp:DropDownList ID="ddlDepartment" runat="server"
DataSourceID="SqlDSDepartment" DataTextField="DepartmentName"
DataValueField="DepartmentID" AppendDataBoundItems="True">
<asp:ListItem Value="" Text=""></asp:ListItem>
</asp:DropDownList>
<asp:SqlDataSource ...>
</asp:SqlDataSource>
</td>
</tr>
<tr>
<td>Access Level</td>
<td>
<asp:DropDownList ID="ddlAccessLevel" runat="server"
DataSourceID="SqlDSAccessLevel" DataTextField="LevelID"
DataValueField="LevelID" AppendDataBoundItems="True">
<asp:ListItem Value="" Text=""></asp:ListItem>
</asp:DropDownList>
<asp:SqlDataSource ...>
</asp:SqlDataSource>
</td>
</tr>
<tr>
<td>
</td>
<td>
<asp:Button ID="btnEdit" runat="server" Text="Submit"
onclick="btnEdit_Click" />
<asp:Button ID="btnReset" runat="server" Text="Reset"
onclientclick="return resetUser();" />
<input id="btnCancel" type="button" value="Cancel" />
</td>
</tr>
</table>
</div></asp:panel>
</div>
</asp:Content>
Here is the code behine in C#, somehow the click event didn't get fired all the time.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Text;
namespace SPR2_v1
{
public partial class EditUsers : System.Web.UI.Page
{
...
protected void lnkUserName_Click(object sender, System.EventArgs e)
{
LinkButton lbUserName = sender as LinkButton;
GridViewRow gvr = (GridViewRow)lbUserName.NamingContainer;
txtFirstName.Text = "";
txtLastName.Text = "";
txtExtension.Text = gvr.Cells[2].Text;
txtEmail.Text = gvr.Cells[3].Text;
ddlCompany.SelectedItem.Text = gvr.Cells[4].Text;
ddlDepartment.SelectedItem.Text = gvr.Cells[5].Text;
ddlAccessLevel.SelectedItem.Text = gvr.Cells[6].Text;
btnEdit.Text = "Update User";
mpeEditUser.Show();
}
protected void btnAddUser_Click(object sender, EventArgs e)
{
btnEdit.Text = "Add User";
}
}
}
Too much code to write, but you need to associate each LinkButton to a ModalPopupExtender during GridView.RowDataBound.
Your real problem though is getting access to the row data to edit. If that is the real goal, see the example below and then you can integrate it with the ModalPopupExtender.
GridView Examples for ASP.NET 2.0: Editing the Underlying Data in a GridView
I recommend you go with a ListView, the templates(EditItemTemplate...etc) are much easier to work with.

Categories