itemtemplate
チェックボックスフィールドを ASP.netに統合しようとして問題が発生しましたgridview
。具体的には、非同期ポストバック後にユーザーが変更したチェックボックスの状態を処理するのに問題があります (おそらくデータバインディングの方法が原因です)。
ページ コンテンツは動的に HTML に導入されるため、非同期ポストバックを使用する必要があることに注意してください<div>
。
レンダリングされたフィールドの中には、SQL データベース フィールドへのユーザー変更を許可する必要がある 2 つのフィールドがあります:IsPrimary
およびIsEnabled
( として表示IsActive
)。
これらのフィールドの状態を正常にレンダリングしています。データベースで IsPrimary が「N」に等しい場合、チェックボックスはオフになり、「Y」に等しい場合はチェックボックスがオンになります。
これはすべて素晴らしいです。ただし、ユーザーがチェックボックスのチェック状態を変更して、非同期ポストバック後に適切なデータベース フィールドを「Y」から「N」に切り替えられるようにしたいと考えています。
私がやりたいことは次のとおりです: ユーザーがチェックボックス (データベース テーブルにバインドされている) の状態を切り替えてからフォームを送信すると、サーバー側のコードが適切なデータベースの変更を行う必要があります。
私の問題は、非同期ポストバックの後に分離コードからフィールドにアクセスしようとすると、ユーザーによって変更されていないように見えることです。つまり、ユーザーが変更した後の状態ではなく、データベースにある状態で表示されます。
私が理解していることから、これは私がデータをにバインドする方法と関係がありgridview
ます。
残念ながら、データを手動でバインドすることに成功していません。
関連するクライアント コードは次のとおりです。
<asp:objectdatasource id="ObjectDataSource2" runat="server" dataobjecttypename="MPads.DataAccess.PatientInsuranceCard"
deletemethod="DeletePatientInsuranceCard" insertmethod="SavePatientInsuranceCard"
selectmethod="ListPatientInsuranceCardsByPatientId" typename="MPads.DataAccess.Repositories.PatientCardRepository"
updatemethod="SavePatientInsuranceCard">
<selectparameters>
<asp:sessionparameter dbtype="Guid" name="patientId" sessionfield="patientId" />
</selectparameters>
</asp:objectdatasource>
<asp:gridview id="GridView1" runat="server" autogeneratecolumns="False" backcolor="White"
bordercolor="White" borderstyle="Ridge" borderwidth="2px" cellpadding="0"
gridlines="None" style="margin-right: 0px" width="695px" datakeynames="PatientInsuranceCardId"
clientidmode="Predictable" viewstatemode="Enabled" datasourceid="ObjectDataSource2">
<columns>
<asp:boundfield datafield="PatientInsuranceCardId" headertext="PatientInsuranceCardId" sortexpression="PatientInsuranceCardId" visible="False"/>
<asp:boundfield datafield="Company" headertext="Company" sortexpression="Company" />
<asp:boundfield datafield="MemberId" headertext="Member ID" sortexpression="MemberId" />
<asp:boundfield datafield="GroupNumber" headertext="Group ID" sortexpression="GroupNumber" />
<asp:boundfield datafield="SubscriberId" headertext="Subscriber ID" sortexpression="SubscriberId" />
<asp:boundfield datafield="DateLastModified" headertext="Last Modified" sortexpression="DateLastModified" />
<asp:templatefield headertext="Primary" sortexpression="IsPrimary">
<itemtemplate>
<asp:checkbox id="chkIsPrimary" runat="server" checked='<%# bool.Parse(Eval("IsPrimary").ToString() == "Y" ? "True": "False") %>'
enabled="true " />
</itemtemplate>
</asp:templatefield>
<asp:templatefield headertext="Active" sortexpression="IsActive">
<itemtemplate>
<asp:checkbox id="chkIsActive" runat="server" checked='<%# bool.Parse(Eval("IsEnabled").ToString() == "Y" ? "True": "False") %>'
enabled="true " />
</itemtemplate>
</asp:templatefield>
フォームを「送信」するクライアント側のコードは次のとおりです。
function loadAjaxContent(pageName, postType, arrayData) {
<div id="pageContent_3" class="content">
<div class="wrap2" style="clear: both; margin-top: 50px;">
<div class="style9" style="clear: both; padding-right: 0px; padding-left: 0px; margin-top: 0px;">
<input id="btnAddMore" type="button" value="Submit insurance" onclick="if(PageValidate()){ loadAjaxContent('InsuranceInformation','POST') };" />
<input id="btnSkip" type="button" value="I don't have insurance (skip)" onclick="loadAjaxContent('EmergencyContacts','POST',null)" />
<input type="hidden" id="isPCP" name="isPCP" value="0" runat="server" />
</div>
</div>
</div>
// Check for post type
if (typeof postType == "undefined")
postType = "GET";
// If no data is passed -- serialize all input,select,textarea fields
// do not pass the viewstate -- will cause errors
if (typeof arrayData == "undefined")
arrayData = $(":input:not(#__VIEWSTATE)").serializeArray();
var targetPage = "../Ajax/" + pageName + ".aspx";
// Show loading badge
$content.hide();
$loading.show();
var ajaxRequest = $.ajax({
url: targetPage,
type: postType,
data: arrayData,
complete: function(result, status) {
var response = $.trim(result.responseText);
// Disable input fields before request is made
$('input').removeAttr('disabled');
// Show appropriate results
if (status == "success") {
$content.html(response);
var hidingOptions = { times: 2 };
var hidingCallback = function() {
$content.show();
if ($(".ContentPage:visible").length > 0) {
$(".ContentPage:visible > .inputfield").first().click();
} else {
$(".inputfield").first().click();
}
};
$loading.hide("pulsate", hidingOptions, 200, hidingCallback);
}
if (document.getElementsByTagName("input").length === 0) {
$(":input").removeAttr("disabled");
}
;
},
error: function() {
$content.load("Ajax/PageNotFound.aspx", function() {
$content.show();
$loading.hide();
$(":input").removeAttr("disabled");
});
}
});
}
;
enter code here
これは、ほとんど無関係で機能しないサーバー側のコードです。
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using MPads.Common;
using MPads.WebPatientPortal.Interfaces;
using MPads.WebPatientPortal.Presenters;
namespace MPads.WebPatientPortal.Ajax
{
public partial class InsuranceInformation : Page, IInsuranceInformation
{
private InsuranceInformationPresenter _presenter;
protected void Page_Init(object sender, EventArgs e)
{
var dataKeys = new ArrayList();
var dataIsPrimary = new ArrayList();
var dataIsActive = new ArrayList();
foreach (GridViewRow row in GridView1.Rows)
{
dataKeys.Add(GridView1.DataKeys[row.DataItemIndex].Value);
dataIsPrimary.Add(((CheckBox)row.FindControl("chkIsPrimary")).Checked);
dataIsActive.Add(((CheckBox)row.FindControl("chkIsActive")).Checked);
System.Diagnostics.Trace.WriteLine("Page_init : Gridview1.rows = " + GridView1.Rows.Count);
}
}
protected void Page_Load(object sender, EventArgs e)
{
var fields = Request.Form;
System.Diagnostics.Trace.WriteLine("fields = "+fields);
_presenter = new InsuranceInformationPresenter();
_presenter.Init(this);
// Get userId
if (fields.Count > 0)
{
System.Diagnostics.Trace.WriteLine("Gridview1.rows = " + GridView1.Rows.Count);
var insuranceName = Utilities.GetNvpValue(fields, "txtInsuranceName");
var planName = Utilities.GetNvpValue(fields, "txtPlanName");
var groupId = Utilities.GetNvpValue(fields, "txtGroupId");
var memberId = Utilities.GetNvpValue(fields, "txtMemberId");
var copayO = Utilities.GetNvpValue(fields, "txtCopayO");
var copayS = Utilities.GetNvpValue(fields, "txtCopayS");
var copayE = Utilities.GetNvpValue(fields, "txtCopayE");
var deductible = Utilities.GetNvpValue(fields, "txtDeductible");
var coinsurance = Utilities.GetNvpValue(fields, "txtCoinsurance");
var memberSince = Utilities.GetNvpValue(fields, "txtMemberSince");
var isPrimary = Utilities.GetNvpValue(fields, "btnInsuranceType");
var dataKeys = new ArrayList();
var dataIsPrimary = new ArrayList();
var dataIsActive = new ArrayList();
foreach (GridViewRow row in GridView1.Rows)
{
dataKeys.Add(GridView1.DataKeys[row.DataItemIndex].Value);
dataIsPrimary.Add(((CheckBox) row.FindControl("chkIsPrimary")).Checked);
dataIsActive.Add(((CheckBox) row.FindControl("chkIsActive")).Checked);
}
_presenter.UpdatePatientInsuranceCard(dataKeys, dataIsPrimary, dataIsActive);
_presenter.SavePatientInsuranceA(insuranceName, planName, groupId, memberId,
copayO, copayS, copayE, deductible, coinsurance, memberSince, isPrimary);
//
}
}
public int GetPageNum()
{
return (1);
}
public void DisplayErrorMessage(string message)
{
//errorMessageContainer.Visible = true;
// errorMessageContainer.InnerText = message;
}
public void loadAjaxContent(string content)
{
Page.RegisterStartupScript("myScript", " <script type=\"text/javascript\"> loadAjaxContent(" + content + ",'POST',null); </script>");
}
public void SetInsuranceLabel(string text)
{
}
}
}
私はこれに少し頭を悩ませており、この問題に対する簡潔で実用的な答えを求めて殺します. DataKey
イベント中にJavaScriptを使用してチェックボックスフィールドを取得し、非表示のテキストボックスに適切な値を入力することを検討しましたonchecked
が、これを達成するための知識がありません(特にIDがasp.netによって破壊されているため)。助けてください :) :(